我在使用PHP的PDO对象准备更新语句和更新记录时遇到问题。我已经采用原始SQL查询并在phpMyAdmin中运行它,并将params替换为传递给函数的值。其中按预期更新记录。但是,从脚本运行时它不会更新。它抛出零错误并返回0000的errorInfo()回复,据我所知,PDO的说法一切都很好。我知道PDO对象有效,因为它成功地插入并从数据库中选择记录,包括我想要更新的记录。我知道这个更新功能很难看,我只是在学习PDO。
显然,这是用PHP5编写的,使用PDO。
班级职能:
public function update($tbl_name, $where = null, $what = null)
{
if(is_array($where))
{
$where_str = 'where ';
foreach($where as $key => $val)
{
$where_str .= "{$key} = ':{$key}' and ";
}
$where_str = substr($where_str,0,-5);
$what_str = 'set ';
foreach($what as $key => $val)
{
$what_str .= "`{$key}` = ':{$key}', ";
}
$what_str = substr($what_str,0,-2);
$query_str = "update {$tbl_name} {$what_str} {$where_str} LIMIT 1;";
$stmt = $this->dbh->prepare($query_str);
echo '<pre>'.print_r($stmt, true).'</pre>';
foreach($what as $key => $val)
{
if('date_time' === $key) continue;
$bind = $stmt->bindValue(":{$key}",$val);
echo ($bind ? 'true' : 'false')." :{$key}=",$val,'<br/>';
}
foreach($where as $key => $val)
{
if('date_time' === $key) continue;
$bind = $stmt->bindValue(":{$key}",$val);
echo ($bind ? 'true' : 'false')." :{$key} ",$val,'<br/>';
}
}else{
return false;
}
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$exec = $stmt->execute();
echo 'exec: '.($exec === true ? 'true:' : 'false:').':'.$exec.'<br/>';
echo '<pre>';
$stmt->debugDumpParams();
echo '</pre>';
return $stmt->errorInfo();
}
从会话更新/登录脚本调用:
$where = array(
'id' => $user['id'],
);
$what = array(
'twitter_key' => $oauth_token,
'twitter_secret' => $oauth_token_secret
);
$update = $db->update('users', $where, $what);
在类函数和调用者中输出echos和print_r:
// print_r($stmt = $this->dbh->prepare($query_str)) output:
PDOStatement Object
(
[queryString] => update users set `twitter_key` = ':twitter_key', `twitter_secret` = ':twitter_secret' where id = ':id' LIMIT 1;
)
// output from the bing params and execution returns
true :twitter_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXX
true :twitter_secret=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
true :id 20
exec: true:1
// $stmt->debugDumpParams() output:
SQL: [111] update users set `twitter_key` = ':twitter_key', `twitter_secret` = ':twitter_secret' where id = ':id' LIMIT 1;
Params: 3
Key: Name: [12] :twitter_key
paramno=-1
name=[12] ":twitter_key"
is_param=1
param_type=2
Key: Name: [15] :twitter_secret
paramno=-1
name=[15] ":twitter_secret"
is_param=1
param_type=2
Key: Name: [3] :id
paramno=-1
name=[3] ":id"
is_param=1
param_type=2
// print_r($stmt->errorInfo()) output:
Array
(
[0] => 00000
)
答案 0 :(得分:2)
我对PDO知之甚少,但我觉得绑定参数的方式有问题。但是,最简单的方法是查看实际查询。
根据docs,您应该能够在$stmt->queryString
中查看生成的查询。现在无法看到,因为您在输出$stmt
之后将参数绑定到语句。
绑定参数后执行print_r()
(或者甚至在执行查询后,我不知道)。你应该得到真正的查询字符串,并找到问题的根源。
答案 1 :(得分:1)
修正了有效的类功能......放置在这里以防万一有人喜欢它,学习,使用或任何东西。
public function update($tbl_name, $where = null, $what = null)
{
if(is_array($where) && is_array($what))
{
$where_str = 'where ';
foreach($where as $key => $val)
{
$where_str .= "{$key} = :{$key} and ";
}
$where_str = substr($where_str,0,-5);
$what_str = 'set ';
foreach($what as $key => $val)
{
$what_str .= "{$key} = :{$key}, ";
}
$what_str = substr($what_str,0,-2);
$query_str = "update {$tbl_name} {$what_str} {$where_str} LIMIT 1;";
$stmt = $this->dbh->prepare($query_str);
foreach($what as $key => $val)
{
if('date_time' === $key) continue;
$bind = $stmt->bindValue(":{$key}",$val);
}
foreach($where as $key => $val)
{
if('date_time' === $key) continue;
if('id' === $key)
{
$bind = $stmt->bindValue(":{$key}",$val, PDO::PARAM_INT);
}else{
$bind = $stmt->bindValue(":{$key}",$val);
}
}
}else{
return false;
}
$stmt->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$exec = $stmt->execute();
return $stmt->errorInfo();
}