2012-10-22 90 views
0

我正在从mysql_connect切换到pdo,无法启动。这里是我的代码:pdo准备好的声明不起作用

//get the row sk 
$sk = strtok($string, "_"); 
//get the column name (the rest of the $string) 
$column_name = substr($string, strpos($string, "_") + 1); 
//get the value 
$value = $_SESSION['save_arr'][$string]; 
echo "{$column_name} {$value} {$sk}</br>"; 
$sql = "update tbl_brand set ?=? where brand_sk=?"; 
$q = $pdo_conn->prepare($sql); 
$q->execute(array($column_name, $value, $sk)); 

,如果我硬编码在某些值,那么它工作正常

$sql = "update tbl_brand set name_long='name' where brand_sk='1'"; 

我敢肯定,这只是一个语法问题,但我无法看到它。我工作过这个例子http://www.phpeveryday.com/articles/PDO-Error-Handling-P552.html

从回声的结果是,像这样:

NAME_LONG国家Autjho玻璃2

回答

7

列名不能在一份声明中绑定到动态值。只有像字符串和整数这样的常量才能被绑定。所以,你的SQL应包含列名它的前准备:

$sql = "update tbl_brand set `$column` = ? where brand_sk = ?"; 

你要确保$列值在SQL语句中嵌入前适当处理。

在MySQL中,你能逃脱列标识符,像这样:

$column = str_replace("`", "``", $column); 
+1

你是一个救星人:)我认为这将会是类似的东西傻......我会选择为最佳答案在5分钟内它会让我 –