php - mysql_num_rows 抛出警告并且不继续

标签 php mysql warnings

这个问题在这里已经有了答案:





mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc... expects parameter 1 to be resource

(31 个回答)


9年前关闭。




我的代码引发了搜索代码的警告。
警告是
SCREAM:忽略错误抑制
( ! ) 警告:mysql_num_rows() 期望参数 1 是资源, bool 值在第 15 行的 C:\wamp\www\test\search.php 中给出

代码是

<html>
    <head>
    </head>
<body>  
<?php
    mysql_connect("localhost","root","") or die (mysql_error());
    mysql_select_db("list") or die (mysql_error());

    if(empty($_POST) === false)
    {
        $data=$_POST['criteria'];


        $get=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where fname='.$data);
        if (mysql_num_rows($get)==0) 
            {
                echo 'There are no search results!!';
            }
                else
            {
                echo '<table border=0 cellspacing=25 cellpadding=1>';
                echo'<tr><th>Sr. No</th><th>First Name</th><th>Last Name</th><th>Phone No</th><th>E-mail</th><th>Address</th><th>Comments!!</th><th>Modify</th><th>Delete!</th></tr>';      
                while($get_row=mysql_fetch_assoc($get))
                    {
                        echo '<tr><td>'.$get_row['SRNO'].'</td><td>'.$get_row['fname'].'</td><td>'.$get_row['lname'].'</td><td>'.$get_row['phone'].'</td><td>'.$get_row['email'].'</td><td>'.$get_row['address'].'</td><td>'.$get_row['comments'].'</td><td><a href="index.php?edit='.$get_row['SRNO'].'">Edit</a></td><td><a href="index.php?delete='.$get_row['SRNO'].'">Delete</a></td></tr>';

                    }
                echo '</table>';
            }
        /*
        if(mysql_num_rows($getf) == 0)
        {
            $getel=mysql_query('SELECT SRNO, fname, lname, phone, email, address, comments from names where lname='.$_GET['$data']));

        }*/
    }
        echo '<form action="" method="post">';  
        echo '<input type="text" name=criteria>';
        echo '<input type="submit" value="search" name="submit">';
        echo '</form>';


?>
</body>
</html>

最佳答案

那是因为您的 sql 失败并且 $get是假的。尝试这个:

$get=mysql_query("SELECT SRNO, fname, lname, phone, email, address, comments from names where fname='" . mysql_real_escape_string($data) . "'");

请注意,我还添加了 mysql_real_escape_string当有人决定使用 sql 注入(inject)攻击您的网站时,这将使您不会哭泣。此外,您应该考虑不使用 mysql_* 函数并切换到准备好的语句,因为这些函数已被弃用。

关于php - mysql_num_rows 抛出警告并且不继续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372255/

相关文章:

javascript - 数据库表更改/更新/删除时触发的 jQuery 事件

php - Youtube API - 提取视频 ID

php - 从给定数组中 id 的数据库中选择

mysql - 将相似的实体存储在同一个表中与数据库中的多个表中

ios - Xcode 13.3 警告 : "' self' refers to the method '{object}.self' , 这可能是意外的

ios - UITabBarController -shouldAutorotateToInterfaceOrientation : warning

postgresql - 使用plpgsql捕获警告

php - 将 td 值传递给模态 html php

php - 如何根据文件上传字段是否选择了文件来运行 php 代码?

MySQL:嵌入式 JSON 与表