0

I have a simple form which when submitted should count the number of rows in a table which already have the same value as that submitted.

I can't work out why this is returning an error... any ideas?

The error is Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in .../register.php on line 31

$con = mysql_connect("table","user","pass");
if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
mysql_select_db("db", $con);    

function check_input($value, $quoteIt)
  {
      // Stripslashes
      if (get_magic_quotes_gpc())
      {
          $value = stripslashes($value);
      }
      // Quote if not a number
      if (is_null($value) || $value=="") {
         $value = 'NULL';
      } else if (!is_numeric($value) && $quoteIt == 1) {
         $value = "'" . mysql_real_escape_string($value) . "'";
      }

      return $value;
  }

$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT * FROM users WHERE email = $useremail";
$result = mysql_query($query);
echo $query;
echo mysql_num_rows($result); //THIS IS LINE 31
mysql_close();
Tom
  • 12,776
  • 48
  • 145
  • 240

4 Answers4

1

You can see your query error using this, because of query error only this error showing

$result = mysql_query($query) or die(mysql_error());
Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • Thanks... Really should get into the habit of adding detailed error reports to my code while I'm developing. Noticed a small typo that was causing the problem. – Tom Jul 04 '12 at 11:38
  • Please do not use this in production!!! Prefer a function log() that write in a file instead of die(), and read your file... – niconoe Jul 04 '12 at 12:51
  • Yeah, of course. I'd thought of that. Thanks though - appreciate the advice. – Tom Jul 05 '12 at 14:54
0
<?php
$con = mysql_connect("table","user","pass");
if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
mysql_select_db("db", $con);    

function check_input($value, $quoteIt)
  {
      // Stripslashes
      if (get_magic_quotes_gpc())
      {
          $value = stripslashes($value);
      }
      // Quote if not a number
      if (is_null($value) || $value=="") {
         $value = 'NULL';
      } else if (!is_numeric($value) && $quoteIt == 1) {
         $value = "'" . mysql_real_escape_string($value) . "'";
      }

      return $value;
  }

$useremail = check_input($_POST['useremail'], 1);
// Check to see if email address already exists in USERS table
$query="SELECT COUNT(1) FROM users WHERE email = $useremail";
$result = mysql_query($query);
$array = mysql_fetch_array($result);
$whatYouWant = $array[0][0];
mysql_close();
?>

But Leigh is right, prefer PDO instead of mysql_* functions...

niconoe
  • 1,191
  • 1
  • 11
  • 25
-1

Usually this means the query is wrong. Try backquotes for fields:

"SELECT * FROM `users` WHERE `email` = '$useremail'"
NoobDev4iPhone
  • 5,531
  • 10
  • 33
  • 33
-1

Replace

$result = mysql_query($query);

with

$result = mysql_query($query, $con);

ChrisG
  • 114
  • 1