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

bsteo
  • 1,606
  • 4
  • 30
  • 52
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. If you choose PDO, [here is a good tutorial](http://j.mp/PoWehJ). Also, don't use `@`. It's a bad coding habbit to just hide errors. – h2ooooooo Nov 29 '13 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 – Mike B Nov 29 '13 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? – h2ooooooo Nov 29 '13 at 13:32

3 Answers3

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)

DonCallisto
  • 27,046
  • 8
  • 62
  • 88
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

Ram Sharma
  • 8,368
  • 7
  • 38
  • 53
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');
Community
  • 1
  • 1