问题描述
我使用此代码更新mysql
中的记录这是我的代码:
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';
}
?>
始终打印successfully
.
但是,当服务器拥挤时,将打印出失败"字符串,而没有结果"变量的任何值.
此查询始终可以正常工作,但有时会返回NULL
.
我该如何解决?
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');
?>
总是成功"打印出来.但有时会打印出错误105".
that 'successfully' always printed out. but sometime 'error 105' printed out.
此错误的原因可能是交易吗?
may be transaction is the reason of this error?
已执行db中的更新,但返回空值?
that update in db is performed but return null?
推荐答案
如果mysql_query返回NULL,那将是PHP上的一个错误.您怎么知道它实际上正在返回NULL?
If mysql_query returned NULL, then that would be a bug on PHP. How do you know that it is actually returning NULL?
对于更新语句,mysql_query应该仅返回TRUE或FALSE.因此,您的错误检查代码很好.至于找出出了什么问题,您将不得不调用其他函数-mysql_error()将使您对出了什么问题有所了解.因此,在您的false块中打印mysql_error()的值.像这样:
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()
执行此操作,您可能会了解记录已更新,但返回值为false"的线索.这本不应该发生的.
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.
这篇关于在mysql_query中更新有时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!