PHP PDO 准备的查询拒绝正确执行 - 转义问题?

发布于 2024-07-25 22:48:55 字数 931 浏览 3 评论 0原文

我在使用 PDO 在 PHP 中准备的查询时遇到问题。 代码:

$link = new PDO("mysql:dbname=$dbname;host=127.0.0.1",$username,$password);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $link->prepare("SELECT locality_name FROM :passedday GROUP BY locality_name ORDER BY locality_name DESC");
$query->bindParam(":passedday",$day); //Where day is, well, a day passed to the script elsewhere
$query->execute();
$result = $query->fetchAll();
$link = null;
//Do things with the $result.

我收到的错误消息是:

SQLSTATE[42000]: 语法错误或访问冲突: 1064 您的 SQL 语法有错误; 检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行的 ''05_26_09' GROUP BY locality_name ORDER BY locality_name DESC' 附近使用的正确语法

当我直接在服务器上执行查询时,它返回适当的结果集,没有任何问题。 有什么想法我做错了吗?

TIA。

编辑:

$day 作为 GET 参数传递。 因此,http://127.0.0.1/day.php?day=05_26_09 会导致 $day = $_GET['day'];

I'm having a problem with a query prepared in PHP with PDO. The code:

$link = new PDO("mysql:dbname=$dbname;host=127.0.0.1",$username,$password);
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = $link->prepare("SELECT locality_name FROM :passedday GROUP BY locality_name ORDER BY locality_name DESC");
$query->bindParam(":passedday",$day); //Where day is, well, a day passed to the script elsewhere
$query->execute();
$result = $query->fetchAll();
$link = null;
//Do things with the $result.

The error message I am getting is:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''05_26_09' GROUP BY locality_name ORDER BY locality_name DESC' at line 1

When I execute the query on the server directly, it returns the appropriate result set without any problem. Any ideas what I'm doing wrong?

TIA.

Edit:

$day is passed as a GET argument. So, http://127.0.0.1/day.php?day=05_26_09 leads to $day = $_GET['day'];.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七禾 2024-08-01 22:48:55

如果 05_26_09 应该押注桌子的名称,那么我猜你遇到了转义问题。 您的本地操作系统与实时服务器不同吗?

我认为您不能将 bindValue()/bindParam() 用于值以外的其他内容(例如表名、字段名)。 所以我有点惊讶它可以在你的本地系统上运行。

If 05_26_09 is supposed to bet the table's name, then I guess you've an escaping problem. Is your local operating system different from the live server?

I don't think you can use bindValue()/bindParam() for something else than values (eg. table name, field name). So I'm a bit suprised, that it works on your local system.

温柔戏命师 2024-08-01 22:48:55

PDO 使用 mysql 的 C-API 来准备语句。
http://dev.mysql.com/doc/ refman/5.0/en/mysql-stmt-prepare.html 说:

标记仅在 SQL 语句中的某些位置是合法的。 [...]但是,它们不允许用于标识符(例如表名或列名)

As a rule of thumb I use: "if you can't wrap it in single-quotes in an ad-hoc query string you can't parametrize it in a prepared statement"

PDO uses mysql's C-API for prepared statements.
http://dev.mysql.com/doc/refman/5.0/en/mysql-stmt-prepare.html says:

The markers are legal only in certain places in SQL statements. [...] However, they are not allowed for identifiers (such as table or column names)

As a rule of thumb I use: "if you can't wrap it in single-quotes in an ad-hoc query string you can't parametrize it in a prepared statement"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文