php错误消息:警告:mysql_num_rows()期望参数1是资源,布尔值

时间:2012-11-08 21:31:24

标签: php ajax

  

可能重复:
  PHP (MySQL) error : “Warning: mysql_num_rows() expects parameter 1 to be resource”

我正试图让这个AJAX函数的php页面起作用。

尝试运行时出现以下错误消息:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/ftpbpan/public_html/ThornAJAX/info.php on line 10

第10行:

**$num_rows = mysql_num_rows($query);**

<?php
$connect = mysql_connect("localhost","****","******") or die("didnot connect to database");

mysql_select_db("people"); 

if(strlen($_GET['user']) >0){
$name = $_GET['user'];
$query = mysql_query("select * from info where name='$name' limit 1");
$num_rows = mysql_num_rows($query);

if($num_rows == 1) {
  $row = mysql_fetch_assoc($query);

1 个答案:

答案 0 :(得分:0)

如果您使用LIMIT 1,您不能假设有1行或0行吗?因此,如果我和你一起使用不推荐的方法与MySQL数据库进行交互,我会使用:

$query = mysql_query("SELECT * FROM info WHERE name='$name' LIMIT 1")or die(mysql_error());
if ($query && mysql_num_rows($query)) {
    // A result was found
    $row = mysql_fetch_array($query);
} else {
    //There were no results found
}

然后,你应该读到这个:

  

Please, don't use mysql_* functions in new code。它们不再被维护,deprecation process已经开始了。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial