-1

可能重复:
mysql_fetch_array() 期望参数 1 是资源,选择中给出的布尔值

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 的行。数据库在脚本的前面已连接。

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

4

3 回答 3

3

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

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

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

 $sql = "SELECT * FROM `apps` WHERE `MCName` = '". $username . "'";
于 2012-08-25T18:55:07.297 回答
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;
于 2012-08-25T19:09:23.590 回答
0
$sql = "SELECT COUNT(*) cn FROM `apps` WHERE `MCName` = '". $username."'";
$result = mysql_query($sql);

然后从中得到你的结果。

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

于 2012-08-25T18:56:22.897 回答