0

I have the following code:

$url = @mysql_fetch_array(mysql_query("SELECT url FROM dl LIMIT 1"));
$url = $url[0];

but I would rather use it as:

$url = @mysql_fetch_array(mysql_query("SELECT url FROM dl LIMIT 1"))[0];

why do I get error for the second one because seems right syntax to me? Or do I miss something?

PHP Parse error: syntax error, unexpected '[' in settings.php on line 52

3
  • 1
    Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial. Also, don't use @. It's a bad coding habbit to just hide errors. Commented Nov 29, 2013 at 13:22
  • php 4 (mysql_) mixed with php5.4 short array syntax :(. It's like 15 years apart between features.. we're never getting rid of mysql_ at this rate Commented Nov 29, 2013 at 13:28
  • @MikeB I don't know if I'm looking forward to, or dread, the day that the current version of PHP doesn't have mysql_ any more. Will we get people that are smart enough to switch to PDO/MySQLi with prepared statements or will everyone simply do a search/replace from mysql_ to mysqli_ and keep all their insecure code? Commented Nov 29, 2013 at 13:32

3 Answers 3

2

First of all

$url = @mysql_fetch_array(mysql_query("SELECT url FROM dl LIMIT 1"))[0];

isn't a valid php syntax(*): array dereferencing isn't supported in php version <= 5.3 (*)

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

As of PHP 5.5 it is possible to array dereference an array literal.

Example #7 Array dereferencing
<?php
function getArray() {
    return array(1, 2, 3);
}

// on PHP 5.4
$secondElement = getArray()[1];

// previously
$tmp = getArray();
$secondElement = $tmp[1];

// or
list(, $secondElement) = getArray();
?>

Second, don't use mysql_* as those functions are deprecated (use mysqli_* or PDO instead)

Sign up to request clarification or add additional context in comments.

3 Comments

It is valid syntax, but only on versions > 5.4.
Yes, my script was written under PHP 5.4 and now I was testing it under PHP 5.3.3, that makes sense.
@BenFortune: I know it, I was integrating my answer ;-)
1

if you are fetching single row than use

mysql_fetch_row 

instead of

mysql_fetch_array

it would be better if you switched to mysqli or pdo. The way you are trying to get the value of array is wrong in php

Comments

1

Using mysql functions is deprecated as of PHP 5.5.0, and will be removed from PHP package soon. Better use MySQLi or PDO to feel safe...
For more info about this topic see: mysqli or PDO - what are the pros and cons?

Anyway. In this situation you can use mysql_result, with three params:

result: Resource from mysql_query()

row: Number of Row, start at 0.

field: The name of the field, or index of that field in resultset.

$url = @mysql_result(mysql_query("SELECT url FROM dl LIMIT 1"), 0, 'url');

Comments

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.