如果在循环中使用MySQLi预准备语句,何时调用bind_param? [英] When do I call bind_param if using MySQLi prepared statements in a loop?

查看:87
本文介绍了如果在循环中使用MySQLi预准备语句,何时调用bind_param?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在MySQLi中使用准备好的语句来插入数据.

I am trying to learn how to use prepared statements with MySQLi to insert data.

即使准备好的语句因有效地重复执行相似的语句而受到赞扬,我似乎也找不到使用MySQLi在循环中执行多个语句的示例.我尤其对以下内容感到困惑:

Even though prepared statements are lauded for their ability to efficiently execute similar statements repeatedly, I can't seem to find examples of executing multiple statements in a loop using MySQLi. I'm especially confused about the following:

  • 在循环之前还是在循环内部调用bind_param
  • 是否在调用bind_param
  • 之前或之后将值赋给我的变量
  • whether to call bind_param before my loop or inside my loop
  • whether to assign values to my variables before or after the call to bind_param

关于准备好的语句的大多数教程都使用PDO.使用PDO,可以将参数值数组传递给execute,而无需调用bindParam. MySQLi并非如此.

Most tutorials on prepared statements use PDO. With PDO, an array of parameter values can be passed to execute, eliminating the need to call bindParam. This is not the case with MySQLi.

PHP手册 mysqli_prepare 条目的示例显示了以下操作顺序:

The PHP manual mysqli_prepare entry has an example that shows the following order of operations:

  1. 将值分配给变量
  2. 准备声明
  3. 绑定变量
  4. 执行
  5. 关闭

根据上述情况,我假设我需要在循环中调用bind语句.

Judging by the above, I had assumed that I would need to call the bind statement within my loop.

但是,PHP手册 mysqli_stmt_execute 条目的示例显示了以下内容操作顺序:

However, the PHP manual mysqli_stmt_execute entry has an example that shows the following order of operations:

  1. 准备声明
  2. 绑定变量
  3. 将值分配给变量
  4. 执行
  5. 将值分配给变量
  6. 执行
  7. 关闭

请注意,此示例未显示的一件事是变量的首次声明位置.我认为将未声明的变量传递给bind_param会产生通知.我终于弄清楚了我可以传递未定义的变量,因为它们是通过引用传递的.

Note that one thing this example doesn't show is where the variables are first declared. I thought passing undeclared variables to bind_param would generate a notice. I finally figured out that I can pass undefined variables because they are passed by reference.

问题:
一个操作顺序比另一个操作顺序优先吗?还是仅取决于您在做什么?彼此之间有麻烦吗?

Question:
Is one order of operations preferred over the other? Or does it just depend on what you are doing? Are there any gotchas with one or the other?

注意:该代码已使用MySQLi,并且我不想切换到PDO(现在切换不在此项目的范围之内).

Note: The code already uses MySQLi and I don't want to switch to PDO (switching now is outside the scope of this project).

推荐答案

由于参数 reference 传递,因此您可以将值赋给调用bind_param后>> .两种方法都可以.

Because the parameters are passed by reference, you can assign the values to the variables before or after the call to bind_param. Either way will work.

如果变量是通过 value 传递的,则每次更改其值时都需要绑定它们.但是,由于它们是通过引用传递的,因此只需绑定一次即可.

If the variables were passed by value, you would need to bind them each time you changed their value. But since they are passed by reference, you only need to bind them once.

在单个执行的情况下,操作顺序并不重要,可能取决于值的来源. 在循环的情况下,您绝对应该在循环之前调用bind_param.

In the case of a single execute, the order of operation doesn't really matter and may depend on where the values are coming from. In the case of a loop, you should definitely call bind_param before the loop.

这篇关于如果在循环中使用MySQLi预准备语句,何时调用bind_param?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆