[英]Weird behaviour when using PHP mysqli row
I am currently translating a PHP script from a PHP server of version 5.4 to a PHP server with version 5.3 我目前正在将PHP脚本从5.4版的PHP服务器转换为5.3版的PHP服务器
I immediately noticed a wierd behaviour in our login script. 我立即在我们的登录脚本中注意到了一种奇怪的行为。
After analysing the script for a bit, I found the source of the problem. 在对脚本进行了一些分析之后,我找到了问题的根源。
A call to a member of the first row in $result->fetch_row was invaild.
调用了$ result-> fetch_row中第一行的成员。
The declaration of $result is shown below: $ result的声明如下所示:
$username = $mysqli->real_escape_string(strtolower($_POST["USERNAME"]));
$password = $mysqli->real_escape_string(md5(strtolower($_POST["PASSWORD"])));
$result = $mysqli->query("SELECT * FROM $table WHERE username='$username' AND password='$password'");
By instinct I checked if I had called the data_seek properly, however; 本能地检查了我是否正确调用了data_seek; I had.
我有
Printing out the fetch_row() function gave me the following result 打印出fetch_row()函数给我以下结果
Array ( [0] => 3 [1] => admin [2] => d76362b0f640fbcf869033b5e4d1c4bf [3] => Mr [4] => Rasmus [5] => 4 [6] => [7] => 0 )
数组([0] => 3 [1] =>管理员[2] => d76362b0f640fbcf869033b5e4d1c4bf [3] => Mr [4] => Rasmus [5] => 4 [6] => [7] => 0)
The array was therefore working. 因此,该阵列正在运行。
But the following declaration did not work. 但是以下声明无效。
$Title = $result->fetch_row()[3];
$ Title = $ result-> fetch_row()[3];
Therefore I tried to store the entire row in a single array object, and then call all sub members individually. 因此,我尝试将整个行存储在单个数组对象中,然后分别调用所有子成员。
Like so: 像这样:
$row = $result->fetch_row();
$Title = $row[3];
And it worked perfectly. 而且效果很好。
How come? 怎么会? What is wrong with this declaration:
该声明有什么问题:
$Title = $result->fetch_row()[3];
$ Title = $ result-> fetch_row()[3];
The ability to reference the elements of a returned array directly from the calling method is introduced in PHP 5.4 - which is why it's not working in 5.3. PHP 5.4中引入了直接从调用方法中引用返回数组元素的功能,这就是为什么它在5.3中不起作用的原因。
From Example #7 on http://php.net/manual/en/language.types.array.php 来自http://php.net/manual/en/language.types.array.php上的示例#7
As of PHP 5.4 it is possible to array dereference the result of a function or method call directly.
从PHP 5.4开始,可以直接对函数或方法调用的结果进行数组取消引用。 Before it was only possible using a temporary variable.
在此之前,只能使用一个临时变量。
So your temporary solution looks like it will become a long-term one :) 因此,您的临时解决方案似乎将成为一个长期解决方案:)
You said you were translating the script from PHP 5.4 to PHP 5.3. 您说过要将脚本从PHP 5.4转换为PHP 5.3。 Apparently you forgot that line because
显然您忘记了那条线,因为
$Title = $result->fetch_row()[3];
is not valid PHP 5.3. 是无效的PHP 5.3。 You should get an error reading:
您应该得到一个错误阅读:
Parse error: syntax error, unexpected '[', expecting ',' or ';'
解析错误:语法错误,意外的[[,期望为','或';' in ...
在...
As you can see in the release notes of PHP.net 如您在PHP.net的发行说明中所见
functionname()[1]
is something what has been added in PHP 5.4. functionname()[1]
是PHP 5.4中添加的内容。
在PHP 5.3和更高版本中不可能直接取消引用函数调用的结果,但是从PHP 5.4开始,这是可能的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.