本文介绍了mysqli :: bind_param();变数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'world');

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$stmt = $mysqli->prepare("INSERT INTO CountryLanguage VALUES (?, ?, ?, ?)");
$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

/* execute prepared statement */
$stmt->execute();

我从 php手册中获取了上述代码令我困惑的部分在这里:

I have taken the above code from the php manual and the parts I am confused about is here:

$stmt->bind_param('sssd', $code, $language, $official, $percent);

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

首先声明变量,然后将其传递给bind_param(),使用此代码是否有效?

Is it valid and equivalent to use this code by first declaring the variables and then passing them to bind_param()?

因此:

$code = 'DEU';
$language = 'Bavarian';
$official = "F";
$percent = 11.2;

$stmt->bind_param('sssd', $code, $language, $official, $percent);

老实说,我不确定手册中的示例为何有效,因为在声明变量之前您正在使用变量.

I am honestly not sure why the example the manual gives is valid at all, as you are using the variable prior to declaring them.

推荐答案

bind_param接受引用,如果您不确定确切引用的内容,请单击此处摘录为解释的参考"

bind_param accepts references, if your unsure what references exactly are then click here for the extract named as "references explained"

这的确意味着,在bind_param之后设置变量是有效的,但同样,这取决于您希望如何进行设置.我一个,更喜欢在绑定之前声明变量

This does mean, it is valid to set your variables after the bind_param but then again, it's down to preference how you wish to do it. I for one, prefer declaring the variables prior to the bind

很少观察

在执行之前以及PHP从上到下处理时声明变量.当确实需要变量(在执行时)时,它们将已经被设置,如果您在execute()之后声明变量,则会提示您SQL错误和其他不必要的php错误

The variables are declared prior to the execute, and as PHP works from top to bottom processing. When the variables are actully needed (on the execution) they would have already been set, if you declare your variables after the execute() you will be prompted with SQL Failures and other unwanted php errors

这篇关于mysqli :: bind_param();变数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:54