mysql_num_rows的PHP错误

时间:2012-07-04 11:24:57

标签: php mysql mysql-num-rows

我有一个简单的表单,在提交时应该计算表中已经具有与提交的值相同的值的行数。

我无法理解为什么这会返回一个错误...任何想法?

错误为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();

4 个答案:

答案 0 :(得分:1)

您可以使用此查看查询错误,因为查询错误仅显示此错误

$result = mysql_query($query) or die(mysql_error());

答案 1 :(得分: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();
?>

但Leigh是对的,更喜欢PDO而不是mysql_ *函数......

答案 2 :(得分:-1)

通常这意味着查询错误。尝试字段的反引号:

"SELECT * FROM `users` WHERE `email` = '$useremail'"

答案 3 :(得分:-1)

替换

$result = mysql_query($query);

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