What value will be returned when a Table got updated?
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.
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 using query()
anyway. There's a function for this; it's called saveField()
and it returns false on failure.
$this->Test->User->id = 999999;
$result = $this->Test->User->saveField('avatar', $c);
if ($result !== false) echo "success";
else echo "failed";
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:
$this->query($q);
Incidentally, if ($result == true)
is redundant and generally considered poor form. just if ($result)
will work identically.
An update query on SQL returns number of rows updated. So it might be returning an integer not bool.
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 ..
精彩评论