占位符使用通用ODBC驱动程序

时间:2009-11-17 20:07:52

标签: php sql odbc insert-update placeholder

目前,我正在提交表格,以说明单引号和&其他垃圾。

 $form_field_value= str_replace("'", "''", stripslashes($form_field_value));

使用以下方法准备插入值:

 $insert_sql = "insert into table (field) values ('".$form_field_value."')";
 odbc_exec($conn, $insert_sql);

基本上,我想为这些插入/更新语句使用占位符。

我尝试将$par1$par2定义为文字,然后执行此

$insert_sql = "insert into table (field,txt) values (?,?)";
odbc_exec($conn, $insert_sql, $par1, $par2);

失败并给了我这个错误:

警告:odbc_exec()[function.odbc-exec]:SQL错误:[Microsoft] [ODBC SQL Server驱动程序] COUNT字段不正确或语法错误,第10行test.php中SQLExecDirect中的SQL状态07001

第10行是exec语句。


我找不到使用此odbc驱动程序的占位符的语法。有什么建议吗?

$conn连接变量工作正常。

<小时/> 修改:

上次尝试仍然失败 - odbc_execute()是一个未定义的函数。我必须使用odbc_exec()

$par1="eggs";
$par2="milk";

$insert_crs = "insert into table (field,txt) values (?,?)";
$stmt = odbc_prepare($conn, $insert_sql); 

odbc_exec($stmt, array($par1, $par2));

1 个答案:

答案 0 :(得分:1)

根据http://www.php.net/manual/en/function.odbc-prepare.php,你应该准备然后执行你的SQL语句,你应该为dobc_execute()的第三个参数提供一个新数组:

<?php
$a = 1;
$b = 2;
$c = 3;
$stmt    = odbc_prepare($conn, 'CALL myproc(?,?,?)');
$success = odbc_execute($stmt, array($a, $b, $c));
?>

这意味着您的代码应如下所示:

$insert_sql = "insert into table (field,txt) values (?,?)";
// the following line is new, compared to your code
$stmt = odbc_prepare($conn, $insert_sql); 
// note that the following line wraps $par1 and $par2 with array()
// struck out version was incorrect - copy/paste error :(
odbc_exec($stmt, $insert_sql, array($par1, $par2));
odbc_execute($stmt, array($par1, $par2));
相关问题