我应该使用mysql_num_rows
( rowCount
in PDO
) inupdate
还是insert
查询?目前,我的代码看起来像这样,
public function update_username(){
$q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
$r = $db->query($q);
if($r){
$message = "Updated successfully";
return $message;
}else{
return false;
}
}
我应该把它改成这样吗?
public function update_username(){
$q = "UPDATE usertable SET username = '$user_name' WHERE id = '$user_id' LIMIT 1";
$r = $db->query($q);
if($r){
$num = $r->rowCount();
if($num == 1){
$message = "Updated successfully";
return $message;
}else{
$message = "An error occurred";
return $message;
}
}else{
return false;
}
}
通常,查询通过没有任何错误,所以我不应该太担心它,但哪一个会是更好的编码实践?或者你有什么建议吗?
提前非常感谢!