当表更新时会返回什么值?
我使用 cakePHP 1.26。
我试图使用以下代码行更新表:
$c = "helloworld";
$q="UPDATE user SET avatar='{$c}' WHERE user_id='999999'";
$result=$this->Test->User->query($q);
if($result==true){echo "success";}
else{echo "failed";}
我注意到表已成功更新,但我仍然看到“失败”消息。
看来 $result 的值既不是 True 也不是 False。
我不知道。
I am using cakePHP 1.26.
I was trying to update a Table using these lines of code:
$c = "helloworld";
$q="UPDATE user SET avatar='{$c}' WHERE user_id='999999'";
$result=$this->Test->User->query($q);
if($result==true){echo "success";}
else{echo "failed";}
I noticed that the Table was updated successfully, but I still saw the "failed" message.
It seems that the value of $result is neither True nor False.
I have no idea.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
表更新后,它返回受影响的行号。
因此,在查询完成后,如果您调用此函数“mysql_affected_rows”,您将获得受影响的行,如果它大于 0,则意味着查询已成功执行,如果为 0,则更新未完成。
after the table is updates it returns the affected rows numbers.
so after your query finished if you would call this function "mysql_affected_rows" you would get the affected rows,and if it is greater than 0 that means the query excecuted succesfully and if it is 0 then the update is not done ..
SQL 上的更新查询返回更新的行数。所以它可能返回一个整数而不是布尔值。
An update query on SQL returns number of rows updated. So it might be returning an integer not bool.
query()
返回 SQL 查询的结果集。您不会得到成功与失败的结果。也就是说,您可能无论如何都不应该使用query()
。有一个函数可以做到这一点;它被称为saveField()
,失败时返回 false。如果您坚持使用
query()
,则没有理由转向其他模型。它只是执行一个 SQL 查询。这和你写的一样有效:顺便说一句,
if ($result == true)
是多余的,通常被认为是糟糕的形式。只是if ($result)
的工作方式相同。query()
returns the result set from the SQL query. You won't get a success vs. failure result. That said, you probably shouldn't be usingquery()
anyway. There's a function for this; it's calledsaveField()
and it returns false on failure.If you insist on using
query()
there's no reason to go to another model. It just executes an SQL query. This would work as well as what you wrote:Incidentally,
if ($result == true)
is redundant and generally considered poor form. justif ($result)
will work identically.