PHP错误:mysql\u num\u rows():提供的参数不是有效的mysql结果

PHP错误:mysql\u num\u rows():提供的参数不是有效的mysql结果,php,Php,我创建了一个php代码,在管理员处显示谁在网站上在线,并存储在网站上连接的用户的ip,当用户10分钟后不再在网站上时从数据库中删除。它的工作,但它向我显示这个错误,我不知道我在哪里出错。有人能帮我做这件事吗 错误是: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a7078942/public_html/online.php on line 17 这是我的代码

我创建了一个php代码,在管理员处显示谁在网站上在线,并存储在网站上连接的用户的ip,当用户10分钟后不再在网站上时从数据库中删除。它的工作,但它向我显示这个错误,我不知道我在哪里出错。有人能帮我做这件事吗

错误是:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a7078942/public_html/online.php on line 17
这是我的代码:

<?php

require "connect.php";
require "functions.php";

// We don't want web bots scewing our stats:
if(is_bot()) die();



$stringIp = $_SERVER['REMOTE_ADDR'];
$intIp = ip2long($stringIp);

// Checking wheter the visitor is already marked as being online:
$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip=".$stringIp);

if(!mysql_num_rows($inDB))
{
    // This user is not in the database, so we must fetch
    // the geoip data and insert it into the online table:

    if($_COOKIE['geoData'])
    {
        // A "geoData" cookie has been previously set by the script, so we will use it

        // Always escape any user input, including cookies:\
        list($city,$countryName,$countryAbbrev) = explode('|',mysql_real_escape_string(strip_tags($_COOKIE['geoData'])));
    }
    else
    {
        // Making an API call to Hostip:

        $xml = file_get_contents('http://api.hostip.info/?ip='.$stringIp);

        $city = get_tag('gml:name',$xml);
        $city = $city[1];

        $countryName = get_tag('countryName',$xml);
        $countryName = $countryName[0];

        $countryAbbrev = get_tag('countryAbbrev',$xml);
        $countryAbbrev = $countryAbbrev[0];

        // Setting a cookie with the data, which is set to expire in a month:
        setcookie('geoData',$city.'|'.$countryName.'|'.$countryAbbrev, time()+60*60*24*30,'/');
    }

    $countryName = str_replace('(Unknown Country?)','UNKNOWN',$countryName);

    // In case the Hostip API fails:

    if (!$countryName)
    {
        $countryName='UNKNOWN';
        $countryAbbrev='XX';
        $city='(Unknown City?)';
    }
    mysql_query("   INSERT INTO tz_who_is_online (ip,city,country,countrycode)
                    VALUES('".$stringIp."','".$city."','".$countryName."','".$countryAbbrev."')");
}
else
{
    // If the visitor is already online, just update the dt value of the row:
    mysql_query("UPDATE tz_who_is_online SET dt=NOW() WHERE ip='".$stringIp."'");
}

// Removing entries not updated in the last 10 minutes:
mysql_query("DELETE FROM tz_who_is_online WHERE dt<SUBTIME(NOW(),'0 0:10:0')");

// Counting all the online visitors:
list($totalOnline) = mysql_fetch_array(mysql_query("SELECT COUNT(*) FROM tz_who_is_online"));

// Outputting the number as plain text:
echo $totalOnline;

?>

我认为这个问题可以通过以下更改解决:

$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip='".$stringIp."'");
但如果此错误再次发生,请更改行:

$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip=".$stringIp);
致:

要查看是什么错误导致了这个问题,请将错误放在注释中


注意:最好在stackoverflow中而不是在serverfault中问这些问题

您的查询有问题
$inDB=mysql\u查询(“从tz\u中选择1 who\u online WHERE ip=“.stringIp”)

tnks它现在可以使用$inDB=mysql\u查询(“从tz\u who\u online中选择1,其中ip=''.$stringIp'.”);
$inDB = mysql_query("SELECT 1 FROM tz_who_is_online WHERE ip=".$stringIp) or die("Error: ". mysql_error(). " with query ". $query);