警告:mysql_num_rows()期望参数1是资源,布尔值在

时间:2012-08-25 18:50:35

标签: php mysql

  

可能重复:
  mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

if (isset($_GET['uid']) && !empty($_GET['uid'])) {
$id = $_GET['uid'];
$sql = "SELECT * FROM `apps` WHERE `id` = ". mysql_real_escape_string($id) ." LIMIT 1";
    if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result) == 1) {
    while($row = mysql_fetch_assoc($result)){
    $username = $row["MCName"];
    echo'

    //Here is a ton of rows and some html.

    ';
    }
    $sql = "SELECT * FROM `apps` WHERE `MCName` = ". $username;
    $result = mysql_query($sql);
    $rows = mysql_num_rows($result);


    }else {
         die('Record not found.');
    }
}
}

我尝试了一切,它一直都在失败。我不是SQL和PHP的主人。我想用“MCName”计算有多少行我不确定我是否可以在第一个SQL查询中执行此操作。 此外,错误出现在第44行,也就是执行mysql_num_rows的行。数据库早先在脚本上连接。

对不起,如果这是一个非常愚蠢的问题,请相信我。我搜索了一个小时左右来修复^^

3 个答案:

答案 0 :(得分:3)

您的第二个查询有SQL语法错误:

 $sql = "SELECT * FROM `apps` WHERE `MCName` = ". $username;

$username变量需要用引号括起来。尝试更新以下内容以修复:

 $sql = "SELECT * FROM `apps` WHERE `MCName` = '". $username . "'";

答案 1 :(得分:1)

这是来自php:http://php.net/manual/en/function.mysql-query.php

Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

有关详细示例,请参阅http://www.php.net/manual/en/mysqli.query.php

正确的方法吗?

$mysqli = new mysqli("localhost", "user", "password", "db");
$sql = "SELECT * FROM `apps` WHERE `MCName` = '%s' ";

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$result = $mysqli->query(sprintf($sql, $mysqli->real_escape_string($username)));
$rows = $result->num_rows;

答案 2 :(得分:0)

$sql = "SELECT COUNT(*) cn FROM `apps` WHERE `MCName` = '". $username."'";
$result = mysql_query($sql);

然后从中得到你的结果。

是的,我建议使用PDO而不是mysql_ *命令