0

继承人的错误:

警告:mysql_num_rows() 期望参数 1 是资源,在第 36 行的 /home/rspslabe/public_html/Toplist/index.php 中给出 null

这是代码:

<?

    $maxdate = time() - ($MaxDays * 86400);

    if ($p == 0)
    {
        $site_info = mysql_query("SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY hits DESC, avrating DESC");
    }
    else if ($p == 1)
    {
        $site_info = mysql_query("SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY avrating DESC, hits DESC");
    }

    $site_num = mysql_num_rows($site_info);
    if ($f == "")
        $f = 0;
    $maxsites = $f + $TopListSize;

    if ($site_num > $maxsites)
        $site_num = $maxsites;

    $i = $f;

它本身的实际网页是这样的:http ://rspslab.com/Toplist/

谢谢,如果有人可以帮忙!:)

4

1 回答 1

1

看到<?,我假设这是完整的代码。如果这是完整的代码...您忘记连接到您的数据库。

您可以使用以下 mysql 代码连接到数据库:

$link = mysql_connect('hostname/ipaddress', 'mysql_user', 'mysql_password');

但是请记住,mysql 已经过时,不应该在新项目中使用,如本网站上的红色所示。相反,我建议使用 mysqli(改进了 mysql)。

要将 mysqli 与您的代码一起使用,您可以尝试以下操作:

$maxdate = time() - ($MaxDays * 86400);
// CONNECT TO THE DATABASE
    $DB_NAME = 'DATABASE_NAME';
    $DB_HOST = 'DATABASE_HOST';
    $DB_USER = 'DATABASE_USER';
    $DB_PASS = 'DATABASE_PASSWORD';

    $mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);

    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    if ($p == 0){
    $query = "SELECT
$Members.title AS title, $Members.url AS url, count(*) AS hits,
avg($IPLogg.vote) AS avrating,
$Members.siteid AS id FROM 
$IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
GROUP BY $Members.siteid ORDER BY hits DESC, avrating DESC";

else if ($p == 1){
            $query = "SELECT
    $Members.title AS title, $Members.url AS url, count(*) AS hits,
    avg($IPLogg.vote) AS avrating,
    $Members.siteid AS id FROM 
    $IPLogg,$Members WHERE $Members.siteid = $IPLogg.siteid AND $IPLogg.time > '$maxdate' 
    GROUP BY $Members.siteid ORDER BY avrating DESC, hits DESC";
}
else{ die("P isn't set?");}

    $result = $mysqli->query($query) or die($mysqli->error.__LINE__);
$maxsites = $f + $TopListSize;
    if($result->num_rows > $maxsites) {
        $site_num = $maxsites;
    }
echo $site_num;

// CLOSE CONNECTION
    mysqli_close($mysqli);

请注意,您必须在插入查询的任何字符串上使用“mysqli_real_escape_string”。通过以下方式执行此操作:

$myescapedstring = $mysqli->real_escape_string($myunescapedstring);

where$myescapedstring是插入到查询$myunescapedstring中的字符串,是需要转义的字符串。

于 2013-02-21T00:02:14.360 回答