2013-01-03 66 views
0

mysql
使用此代码更新记录这是我的代码:更新有时会返回Null

<?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'; 
    } 

?> 

总是successfully打印出来。
但是,当服务器拥挤时,'失败'字符串将被打印出来,'result'变量没有任何值。
此查询总是正常工作,但有时会返回NULL
我该如何解决这个问题?


有一个问题。
我使用此查询的交易。 其实我实际的代码是这样的:

<?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'); 

?> 

是“成功”总是打印出来。但有时会打印出'错误105'。

可能交易这个错误的原因是什么?

在db中执行更新,但返回null?

+0

你写这个来确定查询后是否有更新? –

+0

而不是echo'failed'。$ result;使用echo'failed'.mysql_error(); – Kirtan

回答

0

$导致在这种情况下,仅仅是一种资源,它不包含的原因。

对于其它类型的SQL语句,插入,更新,删除,DROP,等, 的mysql_query()返回TRUE或FALSE的成功上错误。

来源:PHP Manual

所以你只能得到TRUE /从中FALSE在更新查询。它不会告诉你原因。但正如你所说的,当你的服务器是人满为患所以原因可能就是“太多的连接”

+0

谢谢你,我的朋友。 –

1

如果的mysql_query返回NULL它发生,那么这将是在PHP的错误。你怎么知道它实际上返回NULL?

对于更新语句的mysql_query应该只返回TRUE或FALSE。所以你的错误检查代码很好。至于发现哪里出了问题,你将不得不调用其他函数 - mysql_error()会给你一个错误的地方。因此,在false块中打印mysql_error()的值。像这样:

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

这样做,你可能会得到关于'记录是如何更新,但返回值为false'的线索。它不应该发生。

相关问题