所以我正在使用我在书中找到的这个功能,但它似乎不起作用.
/*
Update records in the database
@param String the table
@param Array of changes field => value
@param String the condition
@return Bool
*/
public function updateRecords($table, $changes, $condition){
$update = " UPDATE " . $table . " SET ";
foreach($changes as $field => $value){
$update .= "`" . $field . "`='{$value}',";
}
//remove our trailing ,
$update = substr($update, 0, -1);
if($condition != ""){
$update .= " WHERE " . $condition;
}
$this->executeQuery($update);
return true;
}
Run Code Online (Sandbox Code Playgroud)
我将对象实例化如下:
$a = new Mysqldb($z);
$g = $a->newConnection("localhost", "root", "secrete", "mydatabase");
Run Code Online (Sandbox Code Playgroud)
并使用指定的方法如下:
$a->updateRecords("members", array("id" => 10, "username" => "Paco"), " WHERE id= 10");
Run Code Online (Sandbox Code Playgroud)
但事情不起作用告诉我语法中有错误:
Fatal error: Error executing query: UPDATE members SET `id`='10',`username`='Paco' WHERE WHERE id= 10 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id=10' at line 1 in C:\Users\Robert\Documents\web development\xampp\htdocs\xampp\web_development\MVC\registry\mysqldb.class.php on line 86
Run Code Online (Sandbox Code Playgroud)
所以我在命令行上直接尝试了相同的查询以及反引号,并且它成功了.有什么建议?
更改
$a->updateRecords("members", array("id" => 10, "username" => "Paco"), " WHERE id= 10");
Run Code Online (Sandbox Code Playgroud)
至
$a->updateRecords("members", array("id" => 10, "username" => "Paco"), " id= 10");
Run Code Online (Sandbox Code Playgroud)
这是WHERE两次附加条款
你是另一个附加WHERE在
if($condition != ""){
$update .= " WHERE " . $condition;
}
Run Code Online (Sandbox Code Playgroud)
这是错误的原因.
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |