简体   繁体   中英

update in mysql_query sometime return null

I use this code for update a record in mysql
This is my code:

<?php
    $query = "update seeds set status='success' where id = $x";
    $result = mysql_query($query);
    if(!$result){
         echo 'failed'.$result;
         print_r($result);
    }else{
         echo 'successfully';
    }

?>

always successfully printed out.
But when the server is crowded, the 'failed' string is printed out without any value for 'result' variable.
This query always work correctly but sometime returns NULL .
How can I fix this?


there is a issue.
i use transaction with this query. in fact my actual code is this:

<?php
.
.
.
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
    echo "error 101" ;
    exit;
}
$seed_query = "INSERT INTO seeds VALUES('','$username','$theTime','در انتظار')";
$seed_result = mysql_query($seed_query);
if(mysql_affected_rows()!=1){
    $QUERY_TROLLBACK = mysql_query('ROLLBACK');
    echo "error 102";       
    exit;       
}
$seed_id = mysql_insert_id();                           
$_SESSION['SEED_ID'] = $seed_id;

$query_values = "(NULL,'$list_number[0]',0,$seed_id)";                  
for($i=1;$i<count($list_number);$i++){
    $query_values .= ",(NULL,'$list_number[$i]',0,$seed_id)";
}                
$r_query = "INSERT INTO rc_members VALUES $query_values";
$r_result = mysql_query($r_query);
if(mysql_affected_rows()<=0){
    $QUERY_TROLLBACK = mysql_query('ROLLBACK'); 
    echo "error 103";       
    exit;       
}
$QUERY_COMMIT = mysql_query('COMMIT');
//--------------
$QUERY_TRANSACTION = mysql_query('START TRANSACTION');
if(!$QUERY_TRANSACTION){
    echo "error 104" ;
    exit;
}
$s_status = the_process($list_number,$m,$seed_id);                                          
if($s_status == 'successful_proc'){
    $s_query = "UPDATE seeds SET status='انجام شده' WHERE id=$seed_id";                                                     
    $s_result = mysql_query($s_query);
    //----------------logg file
    $filename = "debug.txt" ;
    $filepointer =fopen($filename ,"w") ;
    fwrite($filepointer , print_r($s_result,true)) ;
    fclose($filepointer) ;              
    //-----------------------
    if(!$s_result){
        echo "error 105".$s_result;                 
        exit;
    }
    $QUERY_COMMIT = mysql_query('COMMIT');
    echo 'successfuly process';
}else{
    $QUERY_TROLLBACK = mysql_query('ROLLBACK');
    echo 'error 106';
    exit;
}
$QUERY_COMMIT = mysql_query('COMMIT');  

?>

that 'successfully' always printed out. but sometime 'error 105' printed out.

may be transaction is the reason of this error?

that update in db is performed but return null?

If mysql_query returned NULL, then that would be a bug on PHP. How do you know that it is actually returning NULL?

For update statements mysql_query should only return TRUE or FALSE. So your error checking code is fine. As to finding out what went wrong, you will have to call other function - mysql_error() would give you a blurb about what went wrong. So print the value of mysql_error() inside your false block. Like this:

 echo 'failed. SQL Err: '. mysql_error()

Do that and you will probably get a clue to as to how 'record got updated, but return value is false'. It should not have happened.

$result in this case is just a resource, it does not contain the reason.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Source: PHP Manual

So you can only get TRUE / FALSE from it in your update query. It won't tell you the reason. But as you said it happens when your server is overcrowded so reason probably is "too many connections"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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