0

I used this on my local server (XAMPP, latest version):

$num_entries = intval( mysqli_fetch_array( $result )[0] );

which worked fine but after deployment my host's PHP is saying:

Parse error: syntax error, unexpected '[' in /home/aaa/public_html/php/Database.php on line 38

I'm guessing this must be due to some retro-compatability issue.

My host is running PHP 5.3.28.

So what's the most efficient way of fetching an array and getting the first element in a single statement?

2
  • I don't think you can do it in one line. Commented Aug 25, 2014 at 14:56
  • Do you need an array? mysqli_fetch_object($result)->{0} Commented Aug 25, 2014 at 14:58

2 Answers 2

4

That syntax is not legal in PHP 5.3, but it is in PHP 5.4 which introduced support for function array dereferencing.

The only option in 5.3 and below is to do it in 2 lines vs one.

$num_entries = mysqli_fetch_array( $result );
$num_entries = intval($num_entries[0]);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the attempted answer but I did mention that in my question. I'm asking for an efficient way of doing it, not why it isn't working! :) Ok for two lines then!
3

You are correct. De-referencing the return of a function as an array was added in 5.4.0.

$row = mysqli_fetch_array( $result );
$num_entries = intval( $row[0] );

1 Comment

But my query is SELECT COUNT(*) so there is only one row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.