dtf579777 2013-01-02 21:59
浏览 67
已采纳

使用PHP mysqli行时的奇怪行为

I am currently translating a PHP script from a PHP server of version 5.4 to a PHP server with version 5.3

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.

The declaration of $result is shown below:

$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; I had.

Printing out the fetch_row() function gave me the following result

Array ( [0] => 3 [1] => admin [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];

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];

  • 写回答

4条回答 默认 最新

  • douao3063 2013-01-02 22:10
    关注

    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.

    From Example #7 on http://php.net/manual/en/language.types.array.php

    As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

    So your temporary solution looks like it will become a long-term one :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题