警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源

Jam*_*mie 2 php mysql

嗨,我收到错误" Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/**/**/locate.php on line 16".

我已经仔细检查了一切,谷歌/ Stackoverflow搜索,无法找出它为什么这样做.不胜感激任何想法!

getdate.php

function getDeals($the_type) {
$result = mysql_query("
    SELECT *
    FROM deals
    WHERE deal_type = '" . $the_type . "'
        ");
}
Run Code Online (Sandbox Code Playgroud)

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>';
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ker 7

您没有从getDeals函数返回结果,因此未在脚本的主体中定义它.

function getDeals($the_type) {
    $result = mysql_query("SELECT * 
                             FROM deals
                            WHERE deal_type = '" . $the_type . "'");
    return $result;
} 
Run Code Online (Sandbox Code Playgroud)

$result = getDeals($type); 
Run Code Online (Sandbox Code Playgroud)

并确保您的$ the_type值经过验证和转义(或者更好,使用PDO),以防止SQL注入