-1

I am using PHP 5.6.12. I retrieve the results of a query from the database, and do as follows

$query = "SELECT `title`, `text`, `url`, `image`, `url_type` FROM `links`";
$result = $conn->query($query);
if(!$result) die($conn->error);
$rows = $result->num_rows;

for($j=0; $j<$rows; ++$j)
{
    $result->data_seek($j);
    $title=$result->fetch_assoc()['title'];
    echo $title;
}

I have this code in a file abc.php. I am developing using Eclipse. In Eclipse, a syntax error is indicated beside the following line - it says:

syntax error, unexpected '['

title=$result->fetch_assoc()['title'];

The strange thing is that if I put the exact same code in a abc.html file and look at it in Eclipse, I don't see any error message in Eclipse.

However, when I look at the php page in a browser, it obtains the value for $title correctly. So why does Eclipse consider this an error? Is there somewhere in Eclipse where I can tell it what version of PHP to use for parsing ?

user2380468
  • 95
  • 1
  • 8
  • What is rows ?, can we see more of your code pls ? – Sofiene Djebali May 20 '16 at 15:52
  • Apart from the fact that the whole thing looks weird to me ... why are you pre-incrementing? `for($j=0;$j<$rows;++$j)` ( *++$j* increments $j then returns it). – CD001 May 20 '16 at 15:53
  • 2
    Array dereferencing introduced in php5.4. Check your php version. – u_mulder May 20 '16 at 15:54
  • php 5.6.12 - normally that should work, right? – user2380468 May 20 '16 at 15:55
  • If you're looking at it as a HTML file, then I assume that Eclipse will just treat the PHP code as text inside a tag and not attempt to parse it; so you can put whatever you like in there. It's only when you tell it it's PHP code that it knows it needs parsing – andrewsi May 20 '16 at 15:56
  • @SofieneDJEBALI $rows is presumably an int variable which stores the amount of rows in the specific database table user2380468 is extracting data from. But Sofiene makes a point, could you post more of your code for us to see? Using [pastebin](http://www.pastebin.com) is fine if the file is too large for the StackOverflow question. – Jonathan Yaniv Ben Avraham May 20 '16 at 15:56
  • I think you just should tell php how to parse your variable: `$title= {$result->fetch_assoc()}['title'];` something like. – u_mulder May 20 '16 at 16:03
  • Within your for should the first line not start with `$result = $result->data_seek($j);` – DrCuddles May 20 '16 at 16:05
  • Ohhhhhh, if you're using eclipse it will use whatever version of PHP you have on your computer. Which More than likely will differ from the server. Google: http://www.eclipse.org/pdt/help/html/php_support.htm – DrCuddles May 20 '16 at 16:06
  • Possible duplicate of [PHP syntax for dereferencing function result](http://stackoverflow.com/questions/742764/php-syntax-for-dereferencing-function-result) – CodeGodie May 20 '16 at 16:17
  • Eclipse for PHP? use PHPstorm or Netbeans instead. – CodeGodie May 20 '16 at 16:19
  • try phpinfo to see the version ! – Mimouni May 20 '16 at 17:11

1 Answers1

2

The problem is that it is necessary to loop through the associative array in this manner:

$title;
while ($data = $result->fetch_assoc()){
       $title = $data;
}
print_r($title)

This example assumes that there is only 1 title (which is printed by print_r($title)). If there are more titles it will simply pick the last one. Also, in the context of the example I can't see which php database interface you use (mysqli or pdo?). Therefore, this example may not work.

markvdlaan93
  • 613
  • 10
  • 26
  • Not necessarily : http://php.net/manual/en/mysqli-result.data-seek.php - what the OP is doing with a `data_seek()` loop is *weird* yes but it's not technically *wrong* – CD001 May 20 '16 at 15:59
  • Yeah, I didn't try the example above. My gut feeling was that particular line was wrong ;). – markvdlaan93 May 20 '16 at 19:25