我在使用PHP的PDO对象准备更新语句和更新记录时遇到问题.我已经采用原始SQL查询并在phpMyAdmin中运行它,并将params替换为传递给函数的值.其中按预期更新记录.但是,从脚本运行时它不会更新.它抛出零错误并返回0000的errorInfo()回复,据我所知,PDO说的一切都很好.我知道PDO对象有效,因为它成功地插入并从数据库中选择记录,包括我想要更新的记录.我理解这个更新功能很难看,我只是在学习PDO.
显然,这是使用PDO在PHP5中编码的.
类功能:
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; …Run Code Online (Sandbox Code Playgroud)