简体   繁体   English

php mysql查询返回true,即使设置为空

[英]php mysql query returns true even when empty set

I have a form that takes a value(id) from the user using html form and looks for it in the database called "account" to display all values. 我有一个表单,该表单使用html表单从用户那里获取一个value(id),并在名为“ account”的数据库中查找它以显示所有值。 If the value entered by the user doesn't exist in the database, it should display an alert. 如果用户输入的值在数据库中不存在,则应显示警报。

//user_form.html

    <html>
    <form method="get" action="user_action.php">
    <input type="text" name="val"/>
    <input type="submit" value="Enter"/>
    </form>
    </html>

//user_action.php
<?php
    $name=$_GET["val"];
    $con = mysql_connect("localhost","my_username","my_password") or die('Could not connect: ' . mysql_error());
    mysql_select_db("account",$con);
    if( ($result=mysql_query("select * from my_table where id=$name")) )
        //display the database table
    else
       echo '<script type="text/javascript">alert("Id not found in database!");</script>';
    mysql_close($con);
?>

My problem is when I enter a value for id that doesn't exist in the database, it doesn't enter the else block and display the alert. 我的问题是,当我输入数据库中不存在的id的值时,它没有输入else块并显示警报。 So, why is the query returning true for all values? 那么,为什么查询对所有值都返回true?

mysql_query() will return true if the query is successful, even if no results are returned. 如果查询成功, mysql_query()将返回true,即使没有返回结果。 You want to use mysql_num_rows to check if any rows are returned. 您想使用mysql_num_rows检查是否返回任何行。

Also, you should really start using PDO or mysqli for your database queries. 另外,您应该真正开始对数据库查询使用PDO或mysqli。 It's a lot more secure is not not deprecated unlike mysql_ functions. 不像mysql_函数那样不被弃用,它的安全性要高得多。

http://us2.php.net/manual/en/ref.pdo-mysql.php http://us2.php.net/manual/zh/ref.pdo-mysql.php

<?php
    $name=$_GET["val"];
    $con = mysql_connect("localhost","my_username","my_password") or die('Could not connect: ' . mysql_error());
    mysql_select_db("account",$con);
    if( ($result=mysql_query("select * from my_table where id=$name")) ) {
       if(mysql_num_rows($result)) {
         // Rows Returned
       }else{
          echo '<script type="text/javascript">alert("Id not found in database!");</script>';
       }
    } else {
      // Mysql Error (Error with Query)   
    }
    mysql_close($con);
?>

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. 对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()成功时返回资源,错误时返回FALSE。

SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()成功返回TRUE,错误返回FALSE。

To check if there are rows returned you need to use 要检查是否有返回的行,您需要使用

mysql_num_rows()

More over you are using deprecated mysql_ function. 您还使用了不推荐使用的mysql_函数。

You should start mysqli_ or PDO with prepared statement. 您应该使用准备好的语句启动mysqli_ or PDO

if( ($result=mysql_query("select * from my_table where id=$name")) )

它应该是

if( ($result==mysql_query("select * from my_table where id=$name")) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM