Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm running PHP on a Windows 8.1 server with IIS. I have a PHP script, and have managed to turn displaying errors on. I found an error on this line.

$hr06status = mysqli_fetch_array(mysqli_query($connection, "SELECT status FROM hours WHERE hour = 6"))['status'];

The exact error is on line 14 Parse error: syntax error, unexpected '['

share|improve this question

closed as off-topic by scrowler, Ja͢ck, andrewsi, Josh Crozier, Ragunath Jawahar Dec 12 '13 at 6:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – scrowler, Ja͢ck, andrewsi, Ragunath Jawahar
If this question can be reworded to fit the rules in the help center, please edit the question.

6  
Try $hr06status = mysqli_fetch_array(mysqli_query($connection, "SELECT status FROM hours WHERE hour = 6")); $hr06status = $hr06status['status']; –  Dave Chen Dec 12 '13 at 1:36
    
Thanks! This fixes the problem, and it appears on the next line. (I have this code 18 times with different numbers.) –  Phoenix Logan Dec 12 '13 at 1:38
    
Or try mysqli_fetch_object(...)->status –  mario Dec 12 '13 at 1:38
    
Array dereferencing was only introduced in 5.4 –  Ja͢ck Dec 12 '13 at 2:29

2 Answers 2

up vote 3 down vote accepted

You trying to do array dereferencing which is only available in PHP 5.4 or later. You are probably running a version older than that so that line of code won't work.

The code in the comment above is the correct way to write it for your version of PHP.

share|improve this answer
    
Pre PHP 5.4: Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL. - manual: php.net/manual/en/language.types.array.php#example-89 –  scrowler Dec 12 '13 at 1:38
    
I will update instead of changing my code. When was PHP 5.4 released? My server has only been running for about 2 weeks. –  Phoenix Logan Dec 12 '13 at 1:40
    
It was released in March 2012 –  John Conde Dec 12 '13 at 1:41
    
I was using 5.3 for some reason... –  Phoenix Logan Dec 12 '13 at 1:44

Indexing on the return value of a function is something PHP added only recently. As Dave Chen says in the comment, you need an interim variable that you can then later index.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.