PHP mysql_query update总是返回true

问题描述:

$result always return true, even though there is no parameter passed to the SQL query.

Everything else is all right and I have tested it in the database.

<?php
     require('dbConnection.php');
    $lon   = $_POST['lon'];
    $lat   = $_POST['lat'];
    $time  = $_POST['time'];
    $date = $_POST['date'];
    $eTime = $_POST['eTime'];
    $eDate = $date;
    $orderID = $_POST['orderID'];


    if($db_found){

        $query = "UPDATE `PostmanLocation` 
        SET `longitude`= '$lon',`latitude`= '$lat',`time`= '$time', `date`='$date'
        WHERE `postID`= '$name'";

        $result=0;

        $result = mysql_query($query) or die("MySQL error:".mysql_error());
        echo $result;
        if($result==1){

                $query = "
                UPDATE `Order` 
                SET `eTime`= '$eTime',`eDate`= '$eDate' 
                WHERE `orderID` = 'orderID'";

                 $result=0;
                $result = mysql_query($query);
                echo $result;
                if($result == 1){
                    $response["success"] = 1;

                } else{  
                    $response["success"] =0;   
                }

        }else{
            $response["success"] = 0;
        }
        echo json_encode($response);
     }
    ?>

$result = mysql_query($query) or die("MySQL error:".mysql_error());

In the instruction above, $result will never be false : either mysql_query returns true, or the script dies.

If you rather want to check if your UPDATE query had any effect at all, you can use this:

Use mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

http://us3.php.net/manual/en/function.mysql-affected-rows.php

Also note that mysql_ functions are deprecated. You should switch to PDO or mysqli.