我一直收到相同的错误消息,无法修复它

时间:2013-02-20 23:33:53

标签: php sql

继承错误:

  

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

这是代码:

<?

    $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/

谢谢,如果有人可以提供帮助! :)

1 个答案:

答案 0 :(得分:1)

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

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

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

请记住,mysql已经过时,不应该在新项目中使用,如This site.上的红色所示。相反,我建议使用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);

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