PDO bindParam 到一个语句中?
有没有一种方法可以将这些 bindParam 语句放入一个语句中?
$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (:username, :email, :password)");
$q -> bindParam(':username', $_POST['username']);
$q -> bindParam(':email', $_POST['email']);
$q -> bindParam(':password', $_POST['password']);
$q -> execute();
我在可能的情况下使用了之前准备的mysqli,我切换到PDO以获得assoc_array支持。在 PDO 的 php.net 网站上,它在单独的行上显示它们,并且在我看到的所有示例中,它都是在单独的行上。
是否可以?
Is there a way I can put these bindParam statements into one statement?
$q = $dbc -> prepare("INSERT INTO accounts (username, email, password) VALUES (:username, :email, :password)");
$q -> bindParam(':username', $_POST['username']);
$q -> bindParam(':email', $_POST['email']);
$q -> bindParam(':password', $_POST['password']);
$q -> execute();
I was using mysqli prepared before where it was possible, I switched to PDO for assoc_array support. On the php.net website for PDO it shows them on seperate lines, and in all examples I have seen it is on seperate lines.
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
execute
页面上的示例 2 就是您想要的:您可能还想看看其他示例。使用问号参数时,将是:
如果这些是唯一的列,您可以编写:
Example 2 on the
execute
page is what you want:You may want to look at the other examples too. With question mark parameters, it would be:
If those are the only columns, you can just write:
辅助函数是一个可以帮助您避免每次要运行查询时编写一堆重复代码的函数。
这就是所谓的“编程”,而这个网站上几乎没有任何此类内容,至少在“PHP”标签下是这样。
虽然许多人认为编程代表从手动示例中复制/粘贴代码块,但它有些不同。
尽管它很难学习,但确实值得,特别是如果您致力于网络开发。
正如您所看到的,没有接受的答案对您没有真正的帮助,因为您仍然需要
在表中编写尽可能多的字段之类的内容,这与您最初的方法没有太大区别,仍然需要您编写每个字段名称四次。
但作为一名程序员,您可以使用编程的力量。例如,循环 - 基础编程运算符之一。
每次看到重复,您就知道应该有一个循环。
例如,您可以设置一个字段列表,仅对它们命名一次。
让程序完成剩下的工作。
比如说,像这样的函数
被赋予一组字段名称,它可以为您生成插入语句和数据数组。以编程方式。所以,你的代码就只剩下这三行:
helper function is a function that makes you help to avoid writing bunch of repetitive code every time you want to run a query.
This is called "programming" and there is almost none of it on this site, at least under "PHP" tag.
While many peiople thinks that programming stands for copy/pasting chunks of code from manual examples, it's somewhat different.
Although it's hard to learn but really worth it, especially if you're devoting yourself to web-developing.
As you can see, no accepted answer did no real help for you, as you still have to write something like
as many times as many fields in your table, which makes not much difference from your initial approach, still makes you write each field name FOUR times.
But being a programmer, you can use powers of programming. A loop, for example - one of cornerstone programming operators.
Every time you see repetitions, you know there should be a loop.
for example, you can set up a list of fields, naming them only once.
And let a program do the rest.
Say, such a function like this one
being given an array of field names, it can produce both insert statement and data array for you. Programmatically. So, your code become no more than these 3 short lines:
您的常识是完全正确的,编码的目的是节省打字...但他的解决方案对 BindParams 位没有帮助。我在网上找不到任何关于此的信息,所以这是我最终说服工作的东西 - 我希望它对某人有用!
然后...
(请参阅PDO::PARAM_INT 在bindParam 中很重要?)
然后,您可以通过
预先清理您的值来插入任何内容。
Your Common Sense is totally right that the aim of coding is to save typing... but his solution doesn't help with the BindParams bit. I couldn't find anything else about this online, so here's something I finally just persuaded to work - I hope it's useful for someone!
Then...
(see PDO::PARAM_INT is important in bindParam?)
Then you can insert anything by doing
Having previously sanitized your values of course.
+1 给 Matthew Flaschen 接受的答案,但我会告诉你另一个提示。如果您使用的 SQL 参数的名称与 $_POST 中的条目相同,则可以利用 $_POST 已经是一个数组的事实:
SQL 参数名称以冒号为前缀 (
:
)但 $_POST 数组中的键不是。但现代版本的 PDO 考虑到了这一点 - 您不再需要在传递给execute() 的数组的键中使用冒号前缀。但您应该小心,任何人都可以向任何 Web 请求添加额外的参数,并且您应该仅获取与查询中的参数匹配的 $_POST 参数的子集。
+1 to Matthew Flaschen for the accepted answer, but I'll show you another tip. If you use SQL parameters with names the same as the entries in $_POST, you could take advantage of the fact that $_POST is already an array:
The SQL parameter names are prefixed with a colon (
:
) but the keys in the $_POST array are not. But modern versions of PDO account for this - you no longer need to use colon prefixes in the keys in the array you pass to execute().But you should be careful that anyone can add extra parameters to any web request, and you should get only the subset of $_POST params that match parameters in your query.
就我个人而言,我更喜欢对所有 pdo 使用包装函数,这可以大大简化所需的代码。
例如,要运行绑定查询(好吧,我的所有查询),我这样做:
请注意,sql 不仅只是一个字符串,而且实际上是一个可重用的字符串,因为您可以简单地将 sql 作为字符串传递并更改如果您想在该变量之后立即执行类似的插入,则传入变量数组(不适用于这种情况,但适用于其他 sql 用例)。
我用来创建这个包装函数的代码如下:
正如注释中所述,您希望使用自己的方法来设置数据库连接并获取初始化的 pdo,但通常它允许您绑定的 sql被削减为只有一行。
Personally, I prefer to use a wrapper function for all of pdo, which simplifies the code necessary substantially.
For example, to run bound queries (well, all my queries), I do this:
Note that not only is the sql simply a string, but it's actually a reusable string, as you can simply pass the sql as a string and change the array of variables to pass in if you want to perform a similar insert right after that one (not applicable to this situation, but applicable to other sql use cases).
The code that I use to create this wrapper function is as below:
As noted in the comments, you'd want to use your own method for setting up a database connection and getting an initialized pdo, but in general it allows your bound sql to be cut down to just a single line.