jjc*_*son 5 php mysql mysqli prepared-statement
我正在尝试创建一个 mysqli 准备好的语句,我将表从 odbc 连接的数据库导入到 mysql 数据库中,我在 106 列宽的表查询中遇到此错误。
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在 '? (ID, column1, column2, column3, column4, '在第 1 行"
当我在此处回显查询时,它是...
插入 ?(ID, column1, column2, column3, column4, ...总共 106 列...) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
$sql = "SELECT * FROM $table WHERE $key = '$acct'";
$link = getODBCConnection();
$result = odbc_do($link, $sql);
$data = array();
while ($row = odbc_fetch_array($result)) {
//store all query rows as array
array_push($data, $row);
}
//insert into mysql table of the same name
//get column count from first row
$columns = count($data[0]);
$params = str_repeat(" ?,",$columns);
$params = rtrim($params,',');
$types = str_repeat("s",$columns+1);
$fields = implode(", ", array_keys($data[0]));
$sql = "INSERT INTO ? ($fields) VALUES ($params) ON DUPLICATE KEY UPDATE";
echo $sql."<br>";
$link = getSalesConnection();
$stmt = $link->prepare($sql);
var_dump($link->error);
foreach ($data as $row) {
$stmt->bind_param($types, $table, implode(", ",array_values($row)));
$stmt->execute();
}
Run Code Online (Sandbox Code Playgroud)
我已经使用标准的 bind_param 和 call_user_func_array() 方法尝试过这个。我试过引用我的参数字符串和列名,但没有效果。如果我的 bind_param 类型有错误,我不应该在准备语句上有错误吗?但是,SQL 转到准备命令时存在一些我无法确定的问题。请帮忙!
查询参数来代替标量值的使用只。您不能参数化表名、列名、SQL 表达式、关键字、值列表等。
错误:SELECT ?, b, c FROM t WHERE a = 1 ORDER BY b ASC
参数值将是文字值,而不是列的名称。
错误:SELECT a, b, c FROM ? WHERE a = 1 ORDER BY b ASC
语法错误。
错误:SELECT a, b, c FROM t WHERE ? = 1 ORDER BY b ASC
参数值将是文字值,而不是列的名称。
错误:SELECT a, b, c FROM t WHERE a IN (?) ORDER BY b ASC
参数值将是单个文字值,而不是值列表,即使您传递一串逗号分隔值也是如此。
错误:SELECT a, b, c FROM t WHERE a = 1 ORDER BY ? ASC
参数值将是文字值,而不是列的名称。
错误:SELECT a, b, c FROM t WHERE a = 1 ORDER BY b ?
语法错误。
基本上,如果您可以编写字符串文字、日期文字或数字文字来代替查询参数,那应该没问题。否则,您必须在准备()之前将动态内容插入到 SQL 字符串中。