This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                                6年前关闭。
            
                    
每次运行代码时,都会遇到此错误,我尝试遍历其他类似线程,但无济于事。我是新手

        $count=3;
            $db = new mysqli ('localhost', 'root', '', 'test');

        $selectSQL='SELECT * FROM coll_mark WHERE univ="'.$_POST['univ'].'" AND '.$brachTxt.'<="'.$_POST['perc'].'"AND '.$brachTxt.'!=""  ORDER BY '.$brachTxt.' DESC LIMIT '.$count;
        $queryset='';
        $queryset=mysql_query($selectSQL);
        while($row = mysql_fetch_assoc($queryset))
        {
        echo('<tr><td id="ColgNames">'.$row['name'].'</td><td align="center">'.$row[$brachTxt].'</td></tr>');
        }


我还检查了查询结构是否正确,但是查询运行正常。

最佳答案

像这样尝试

$count=3;
$db=mysql_connect('localhost','root','');
if(!$db) {
    die('Could not connect: '.mysql_error());
}
$connection_string=mysql_select_db('test',$db);
$selectSQL='SELECT * FROM coll_mark WHERE univ="'.$_POST['univ'].'" AND '.$brachTxt.'<="'.$_POST['perc'].'"AND '.$brachTxt.'!="" ORDER BY '.$brachTxt.' DESC LIMIT '.$count;
$queryset=mysql_query($selectSQL);
$num=mysql_num_rows($queryset);
if(0==$num) {
    echo "No record";
    exit;
} else {
    while($row=mysql_fetch_assoc($queryset)) {
        echo('<tr><td id="ColgNames">'.$row['name'].'</td><td align="center">'.$row[$brachTxt].'</td></tr>');
    }
}

关于php - 警告:mysql_fetch_assoc()期望参数1为资源, bool 值在[duplicate]中给出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20423037/

10-11 03:31