PHP中的ODBC准备语句 [英] ODBC prepared statements in PHP

查看:61
本文介绍了PHP中的ODBC准备语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在PHP中使用odbc_prepare和odbc_execute,如下所示:

I'm trying to use odbc_prepare and odbc_execute in PHP as follows:

$pstmt=odbc_prepare($odb_con,"select * from configured where param_name='?'");
$res=odbc_execute($pstmt,array('version'));
var_dump($res);  //bool(true)
$row = odbc_fetch_array($pstmt);
var_dump($row);  //bool(false)

第一个var_dump返回true,因此执行成功,但是没有返回行.确实存在带有param_name ='version'的行.为什么没有返回行?

The first var_dump returns true so the execute succeeds, but there is no row returned. A row does indeed exist with the param_name = 'version'. Why is no row returned?

为了使事情变得有趣,我使用准备好的插入符在php中运行了另一个非常简单的示例.

To make things interesting, I ran another very simple example in php using a prepared insert.

$pstmt=odbc_prepare($odb_con,"insert into tmp1 values(?,'?')");

此行本身就在数据库中插入了一行!当然这是错误的吗?输入的数据为col 1 =空白,col 2 =?

This line, by itself, inserted a row into the database!! Surely this is just wrong? The data entered was col 1 = blank, col 2 = ?

对于从何处开始解决此问题的任何建议,我们将不胜感激.

Any advice on where to start fixing this would be appreciated, thanks.

这是在PHP 5.2.8中

This is in PHP 5.2.8

推荐答案

尝试从查询字符串中删除单引号并将其添加到参数值本身中:

Try removing the single quotes from the query string and adding them to the parameter value itself:

$pstmt=odbc_prepare($odb_con,"select * from configured where param_name=?");
$res=odbc_execute($pstmt,array(" 'version'"));
var_dump($res);  //bool(true)
$row = odbc_fetch_array($pstmt);
var_dump($row);  //bool(false)

参数值开头的单个空格字符非常重要-如果空格不存在,它将把变量视为文件的路径.

The single space character at the beginning of the parameter value is very important--if the space is not there, it will treat the variable as a path to a file.

来自 http://www.php.net/manual/en /function.odbc-execute.php :

如果您想存储一个字符串 实际上以单身开始和结束 引号,您必须添加一个空格或其他 非单引号字符 参数的开头或结尾, 这将阻止参数 作为文件名.

If you wish to store a string which actually begins and ends with single quotes, you must add a space or other non-single-quote character to the beginning or end of the parameter, which will prevent the parameter from being taken as a file name.

这篇关于PHP中的ODBC准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆