使用ODBC + Access时在SQL查询中转义输入数据

编程入门 行业动态 更新时间:2024-10-21 19:36:36
本文介绍了使用ODBC + Access时在SQL查询中转义输入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经尝试过odbc_prepare() + odbc_execute()来更新Access文件中的记录,但是我总是收到关于列数不正确的SQL state 07001错误消息(实际上,该消息使用的是Spanglish,并且收效不大感):

I've tried odbc_prepare() + odbc_execute() to update a record in an Access file but I always get an SQL state 07001 error message about incorrect column count (actually, the message is in Spanglish and doesn't make much sense):

<?php $items = array(); $items[100] = 'Foo'; $items[200] = 'Bar'; $sql = 'UPDATE street SET name=? WHERE street_id=?'; $stmt = odbc_prepare($conection, $sql); if( $stmt===FALSE ){ die(odbc_errormsg()); } foreach($items as $cod => $name){ if( !odbc_execute($stmt, array($name, $cod)) ){ die(odbc_errormsg()); } }

odbc_execute手册页上的用户评论表明Microsoft Access ODBC驱动程序不支持参数化查询.但是,我还没有找到odbc_ *函数来转义数据.

User comments at odbc_execute manual page suggest that Microsoft Access ODBC drivers do not support parameterized queries. However, I haven't found an odbc_* function to escape data.

那么...我该如何转义输入数据?

So... How can I escape input data?

推荐答案

我一直在尝试随机的东西.如果您使用以下一种语法(甚至混合使用),似乎odbc_prepare()会检测到参数:

I've been trying out random stuff. It seems that odbc_prepare() detects parameters if you use one of these syntaxes (or you even mix them):

  • INSERT INTO foo (bar) VALUES (:param)
  • INSERT INTO foo (bar) VALUES ([param])
  • INSERT INTO foo (bar) VALUES (:param)
  • INSERT INTO foo (bar) VALUES ([param])

但是,odbc_execute()会抱怨缺少参数,无论您用什么来喂(数值数组,关联数组...).并且它将知道无法找到的确切参数数量.这使得整个机制完全没有意义.

However, odbc_execute() will complain about missing parameters no matter what you feed it with (numeric array, associative array...). And it'll know the exact number of parameters that cannot be found. That makes the whole mechanism completely pointless.

不幸的是,我的最好的解决办法,到目前为止是这样的:

Sad to say, my best solution so far is this:

/** * Escape a string to be inserted into Access via ODBC */ function odbc_escape_string_access($value){ $replacements= array( "'" => "''", ); return strtr($value, $replacements); }

这太可怕了,但我找不到更好的东西.

It's horrible but I couldn't find anything better.

更多推荐

使用ODBC + Access时在SQL查询中转义输入数据

本文发布于:2023-10-25 08:23:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   ODBC   Access   SQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!