关联数组和绑定参数

发布于 2024-07-17 03:11:30 字数 912 浏览 3 评论 0原文

我对 PDO::prepare 函数有点困惑。

我有这样的东西

array('user_email'=>'[email protected]','user_pass'=>'password')

,我想

INSERT INTO user_info (user_email, user_pass) VALUES ([email protected], password)

使用 PDO (或 mysqli,我愿意接受建议)的参数化查询将其转换为这样的东西。 另一个想法 -

array('uid'=>'10', 'first_name'=>'robert', 'last_name'=>'jones')
array("email", "number")

我知道

SELECT email, number FROM t1 WHERE uid=10 AND first_name=robert AND last_name=jones

答案位于 PDO::preparecall_user_func_array 的某个地方,但我对后一个函数的工作原理感到非常困惑,并且希望有一个解释。

I've gotten a little confused with the PDO::prepare functions.

I have something like this

array('user_email'=>'[email protected]','user_pass'=>'password')

and i'd like to translate it into something like this

INSERT INTO user_info (user_email, user_pass) VALUES ([email protected], password)

using parameterized queries with PDO (or mysqli, I'm open to suggestions).
Another idea -

array('uid'=>'10', 'first_name'=>'robert', 'last_name'=>'jones')
array("email", "number")

into

SELECT email, number FROM t1 WHERE uid=10 AND first_name=robert AND last_name=jones

I know the answer lies somewhere with PDO::prepare and call_user_func_array, but I've gotten really confused on how the latter function works, and would appreciate an explanation.

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

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

发布评论

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

评论(3

可爱咩 2024-07-24 03:11:30

我很困惑,也许你也是。 这是一个简单的例子:

$sth = $dbh->prepare('SELECT * FROM table WHERE id = ? AND date = ?');
$sth->execute(array(150, '2009-04-04'));
$data = $sth->fetchAll();

或:

$sth = $dbh->prepare("INSERT table VALUES(:foo, :bar)");
$sth->bindParam(":foo", $foo);
$sth->bindParam(":bar", $bar);

或:

$sth = $dbh->prepare("INSERT INTO user_info (user_email, user_pass) VALUES (:email, :pass)");
$sth->execute(array(':email' => '[email protected]', ':pass' => '1234'));

希望这有帮助!

I'm confused, and maybe you are too. Here is a simple example:

$sth = $dbh->prepare('SELECT * FROM table WHERE id = ? AND date = ?');
$sth->execute(array(150, '2009-04-04'));
$data = $sth->fetchAll();

Or:

$sth = $dbh->prepare("INSERT table VALUES(:foo, :bar)");
$sth->bindParam(":foo", $foo);
$sth->bindParam(":bar", $bar);

Or:

$sth = $dbh->prepare("INSERT INTO user_info (user_email, user_pass) VALUES (:email, :pass)");
$sth->execute(array(':email' => '[email protected]', ':pass' => '1234'));

Hope this helps!

恬淡成诗 2024-07-24 03:11:30

PDOStatement::execute() 使用参数标记,因此您必须在调用 PDO::prepare() 之前构造查询。

PDOStatement::execute() works with parameters markers, so you have to construct query before calling PDO::prepare().

神魇的王 2024-07-24 03:11:30

您不必使用 call_user_func_array()。 PDOStatement::execute() 默认采用关联数组。

$stmt = $pdo->prepare("SELECT fld FROM tbl WHERE fld=:parameter1 AND fld2=:parameter2");
$stmt->execute(array(":parameter1" => "value1", ":parameter2" => "value2"));
...

http://se.php.net/manual/en/pdo.prepare。 php

You don't have to use call_user_func_array(). PDOStatement::execute() takes associative arrays by default.

$stmt = $pdo->prepare("SELECT fld FROM tbl WHERE fld=:parameter1 AND fld2=:parameter2");
$stmt->execute(array(":parameter1" => "value1", ":parameter2" => "value2"));
...

http://se.php.net/manual/en/pdo.prepare.php

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