如何在此查询中使用准备好的语句?

发布于 2024-08-16 11:27:59 字数 1103 浏览 4 评论 0原文

我是 PHP 和 PDO 的新手,我尝试在这里使用准备好的语句。经过 1 个小时的尝试后我放弃了。或者我的教程非常糟糕。

编辑:

在没有准备好的语句的情况下,这可以完美地工作:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
    $prepared = $dbh->prepare('SELECT * from sys_navigation_point WHERE name="root"');
    //$prepared->bindParam('foo', 'root');

    $prepared->execute();

    foreach($prepared as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

但这对于准备好的语句根本不起作用。执行此操作时出现完全空白的页面:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
    $prepared = $dbh->prepare('SELECT * from sys_navigation_point WHERE name=:foo');
    $prepared->bindParam('foo', 'root');

    $prepared->execute();

    foreach($prepared as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

foo 应替换为 root。然而,事实并非如此。

I'm new to PHP and PDO, and I try to use prepared statements here. After 1 hour of trying around I give up. Or my tutorial was just horribly bad.

EDIT:

This works perfectly without prepared statements:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
    $prepared = $dbh->prepare('SELECT * from sys_navigation_point WHERE name="root"');
    //$prepared->bindParam('foo', 'root');

    $prepared->execute();

    foreach($prepared as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

But this does not work at all with a prepared statement. Getting a totally blank page when doing this:

try {
    $dbh = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
    $prepared = $dbh->prepare('SELECT * from sys_navigation_point WHERE name=:foo');
    $prepared->bindParam('foo', 'root');

    $prepared->execute();

    foreach($prepared as $row) {
        print_r($row);
    }
    $dbh = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

foo should be replaced with root. However, it doesn't.

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

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

发布评论

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

评论(5

━╋う一瞬間旳綻放 2024-08-23 11:27:59

绑定时也尝试在名称中使用冒号:

$prepared->bindParam(':foo', 'root');

正如文档中所做的那样: http ://php.net/manual/en/pdostatement.bindparam.php

Try using the colon in the name, too while binding:

$prepared->bindParam(':foo', 'root');

As it is done in the docs: http://php.net/manual/en/pdostatement.bindparam.php

梦断已成空 2024-08-23 11:27:59

bindParam 的第二个参数必须 是一个变量,否则您将收到致命错误。所以,

$value='root';
$prepared->bindParam('foo', $value);

或者:

$prepared->bindValue('foo', 'root');

很容易弄清楚何时显示错误消息:

if ($in_development) ERROR_REPORTING(E_ALL);
// ... code

Your bindParam's second parameter has to be a variable, otherwise you'll get a fatal error. So,

$value='root';
$prepared->bindParam('foo', $value);

or:

$prepared->bindValue('foo', 'root');

It's easy to figure out when error messages are displayed:

if ($in_development) ERROR_REPORTING(E_ALL);
// ... code
横笛休吹塞上声 2024-08-23 11:27:59

您不能将参数用于表和列名称之类的内容,它仅用于数据,而不用于完全动态查询

这应该有效:

$prepared = $dbh->prepare('SELECT * from sy_navigation_point WHERE Foo=:whatever');
$prepared->bindParam('whatever', 'Bar');

编辑:这应该是真正的解决方案。

通过查看文档,很明显该模式具有成为:

$prepared = $dbh->prepare('SELECT * from sy_navigation_point WHERE Foo=:whatever');
$prepared->bindParam('whatever', $value);

那么你就:

$value = 'Bar';
$prepared->execute();

You can't use params for stuff like table and column names, it's meant to be used for data only, not for fully dynamic queries

This should work:

$prepared = $dbh->prepare('SELECT * from sy_navigation_point WHERE Foo=:whatever');
$prepared->bindParam('whatever', 'Bar');

EDIT: This should be the real solution.

By looking at the documentation, it's clear that the pattern has to be:

$prepared = $dbh->prepare('SELECT * from sy_navigation_point WHERE Foo=:whatever');
$prepared->bindParam('whatever', $value);

Then you do:

$value = 'Bar';
$prepared->execute();
与风相奔跑 2024-08-23 11:27:59

http://www.php.net/manual/en/pdo.prepare。 php
那里的评论者说它不适用于关键字、表名称、视图名称和字段名称
所以你需要 $prepared = $dbh->prepare('SELECT * from ' . $table);

因为它只适用于列变量。

http://www.php.net/manual/en/pdo.prepare.php
A commenter there says that it doesn't work properly for keywords, table names, view names and field names
So you'd need $prepared = $dbh->prepare('SELECT * from ' . $table);

As it only really works for column variables.

浮光之海 2024-08-23 11:27:59

您不能在 MySQL 准备好的语句中绑定表,只能绑定值。来自手册

但是,它们不允许用于标识符(例如表名或列名),
或者指定二元运算符的两个操作数,例如 = 等号。

You cannot bind a table in a MySQL prepared statement, you can only bind values. From the manual:

However, they are not allowed for identifiers (such as table or column names),
or to specify both operands of a binary operator such as the = equal sign.

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