0

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

Hey I am making a unique hit counter.

<?php  
$ip_add = $_SERVER['REMOTE_ADDR'];

function ip_exists($ip){
  global $ip_add;
  $query = "SELECT 'ip_count' from 'count_ip' where 'ip_count' = '$ip_add'";
  $query_run = mysql_query($query);
  $query_num_rows = mysql_num_rows($query_run);

  if($query_num_rows == 0){
    return false;
  }
  else if($query_num_rows >= 1){
    return true;
  }
}

if(ip_exists($ip_add)) {
  echo " exists";
} else {
  echo "doesnt";
}
...

I am getting this Error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in 
C:\wamp\www\php\Hit Counter\index.php on line 18

The error is on here:

$query_num_rows = mysql_num_rows($query_run);
Community
  • 1
  • 1
Jasminder Pal Singh
  • 524
  • 2
  • 7
  • 24
  • what is the table structure of count_ip table? – Adam May 15 '12 at 18:38
  • Please stop writing new code with the ancient `mysql_*` functions. They are no longer maintained and community has begun the [deprecation process](http://news.php.net/php.internals/53799). Instead you should learn about prepared statements and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you care to learn, [here is a quite good PDO-related tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Madara's Ghost May 15 '12 at 18:39

4 Answers4

2

Firstly, we see in the error message that there was a boolean given - the only reason mysql_query() returns a boolean for a SELECT query is if the query failed - which it will have done because you have a syntax error in your SQL. You need to change some of the single quotes to backticks:

$query = "
  SELECT `ip_count`
  FROM `count_ip`
  WHERE `ip_count` = '$ip_add'
";

Object names (columns, tables etc) should be quoted with backticks `, anything surround with single quotes ' is treated as a literal string.

You need to check for the failure of the query:

$query_run = mysql_query($query);
if (!$query_run) {
  // Handle error here
}
$query_num_rows = mysql_num_rows($query_run);

Some additional comments:

DaveRandom
  • 86,228
  • 11
  • 149
  • 173
1

Test your query to make sure it is being executed properly:

<?php
...
$query_run = mysql_query($query);
if (!$query_run) {
  die('Invalid query: ' . mysql_error());
} else {
  // continue your code
}
?>

Like this, if the query fails, you can obtain some feed back to analyze!

See this: mysql_query Example, #1 Invalid Query

Zuul
  • 15,997
  • 6
  • 59
  • 87
0

mysql_query() returns a boolean, whether the query succeeded or not. If you want to pass in the correct argument, you need to assign the connection handler to a variable, and pass that in.

$conn = mysql_connect()...
...
$query_num_rows = mysql_num_rows($conn);

Though you should not use mysql_* functions anymore! See my comment on your question!

Madara's Ghost
  • 165,920
  • 50
  • 255
  • 304
0

Actually the query has syntax error which causes the $query_run() to return an error instead of the actual output. You have to be careful while using the quotes in queries. replace the query like this:

$query = "SELECT ip_count from count_ip where ip_count='$ip_add'";

If $ip_add is not a string, avoid single quotes there too. avoid quotes if you are writing a field name or a table name. You need a quote only where you write a string.

Harikrishnan
  • 7,219
  • 12
  • 59
  • 110