mysql query更新,在mysql_query中更新有时返回null

本文探讨了使用MySQL事务处理及错误排查的方法。作者通过具体的PHP代码示例,展示了如何利用事务确保数据一致性,并介绍了在遇到更新记录失败时的调试技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I use this code for update a record in mysql

This is my code:

$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:

.

.

.

$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

$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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值