简体   繁体   English

为什么此聚合函数查询给出语法错误?

[英]Why is this aggregate function query giving syntax error?

I'm trying to run this code on actual server but it's giving syntax error whereas the same query works perfectly on my localhost. 我正在尝试在实际服务器上运行此代码,但它给出语法错误,而同一查询在我的本地主机上运行良好。 I tried several possibilities but no luck. 我尝试了几种可能性,但没有运气。 Can anyone tell me what's the problem? 谁能告诉我这是什么问题?

<?php
    $connection = new mysqli("localhost", "username", "password", "database");

    $first_id = $connection->query("SELECT MIN(id) AS first_id FROM sample")->fetch_array(MYSQLI_ASSOC)['first_id'];
echo $first_id;

?>

This is the syntax error I'm getting. 这是我得到的语法错误。

Parse error: syntax error, unexpected '[' on line 5 解析错误:语法错误,第5行出现意外的[[]

You are doing array dereferencing which is only available in PHP 5.4+. 您正在执行数组解引用 ,这仅在PHP 5.4+中可用。 You are not running PHP 5.4+. 您没有运行PHP 5.4+。

change 更改

$first_id = $connection->query("SELECT MIN(id) AS first_id FROM sample")->fetch_array(MYSQLI_ASSOC)['first_id'];

to: 至:

$first = $connection->query("SELECT MIN(id) AS first_id FROM sample")->fetch_array(MYSQLI_ASSOC);
$first_id = $first['first_id'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM