为什么这个聚合函数查询会出现语法错误?

时间:2014-04-03 13:59:47

标签: php mysql sql

我试图在实际服务器上运行此代码,但是它会给出语法错误,而相同的查询在我的localhost上完美运行。我尝试了几种可能但没有运气。谁能告诉我这是什么问题?

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

?>

这是我得到的语法错误。

解析错误:语法错误,意外&#39; [&#39;在第5行

1 个答案:

答案 0 :(得分:5)

您正在执行array dereferencing,仅适用于PHP 5.4+。你没有运行PHP 5.4 +。

变化

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

为:

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