简体   繁体   中英

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

You are doing array dereferencing which is only available in PHP 5.4+. You are not running 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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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