[英]Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Hi I'm receiving the error " Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/**/**/locate.php on line 16
". 嗨,我收到错误“
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/**/**/locate.php on line 16
”。
I've double checked everything, Googled/Stackoverflow searched and can't find out why its doing this. 我已经仔细检查了所有内容,进行了Googled / Stackoverflow搜索,但找不到原因。 Would appreciate any thoughts!
将不胜感激!
getdate.php getdate.php
function getDeals($the_type) {
$result = mysql_query("
SELECT *
FROM deals
WHERE deal_type = '" . $the_type . "'
");
}
locate.php?type=fun locate.php?类型=乐趣
$type = $_GET['type'];
include("getdata.php");
getDeals($type);
if (mysql_num_rows($result)) {
echo '<ul>';
while($row = mysql_fetch_array($result))
{
echo '<a href="deal.php?i=' . $row["i"] . '">';
echo '<li class="deal ' . $row["deal_type"] . 'deal">';
echo '<h3>' . $row["deal_title"] . '</h3>';
echo '</li>';
echo '</a>';
}
echo '</ul>';
}
else {
echo '<div class="nodeals">None</div>';
}
You're not returning the result from your getDeals function, so it isn't defined in the main body of the script. 您不会从getDeals函数返回结果,因此未在脚本主体中定义结果。
function getDeals($the_type) {
$result = mysql_query("SELECT *
FROM deals
WHERE deal_type = '" . $the_type . "'");
return $result;
}
and 和
$result = getDeals($type);
and ensure that your $the_type value is validated and escaped (or better yet, use PDO), to prevent SQL injection 并确保您的$ the_type值经过验证和转义(或者更好的是使用PDO),以防止SQL注入
You need to import the $result into the function as a global: 您需要将$ result作为全局变量导入到函数中:
function getDeals($the_type) {
global $result;
$result = mysql_query("
SELECT *
FROM deals
WHERE deal_type = '" . $the_type . "'
");
}
However, this is not a good way to set this type of thing up. 但是,这不是设置此类事件的好方法。 You should instead use something like PDO or return a proper resource identifier.
您应该改用PDO之类的东西或返回适当的资源标识符。
There a number of ways to fix this, however the best way is to return the mysql resource and then assign that resource to the $result
variable. 有很多方法可以解决此问题,但是最好的方法是返回mysql资源,然后将该资源分配给
$result
变量。
function getDeals($the_type) {
return mysql_query("
SELECT *
FROM deals
WHERE deal_type = '" . $the_type . "'
");
}
$result = getDeals($type);
As a rule of thumb, 99% of errors are due to given or the immediately previous line or statement. 根据经验,有99%的错误是由于给定的或紧邻的前一行或语句。 In your case, others noticed it first, that
getDeals($type)
does not return the result. 对于您的情况,其他人首先注意到了这一点,即
getDeals($type)
不会返回结果。
You should also change the call to mysql_query()
to something like: 您还应该将对
mysql_query()
的调用更改为以下内容:
function getDeals($the_type) {
$query = "
SELECT *
FROM deals
WHERE deal_type = '" . $the_type . "'
";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
return $result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.