0

This is not quite the same as the other dupes!

I have a web service that grabs data based on a token passed in

$query = "SELECT * FROM Journey WHERE Token='{$token}';";
$result = mysql_query($query,$link) or die('Errant query:  '.$query);
$posts = array();
$dto = new Journey();
$dta = new Dta();
if(mysql_num_rows($result) > 0) {
  while($post = mysql_fetch_assoc($result)) {
    $posts[] = $post;
    $posts = array_shift($posts);

When I run the service and pass in a valid token, I get the fatal error at $posts[] = $post

From what I can see (and have read), this looks to be the right way to do this (and indeed, I have it in my other web services and it works there)

Using PHP 5.6.3

Nodoid
  • 1,449
  • 3
  • 24
  • 42
  • 5
    You should not be using those old `mysql_` functions anymore. Have you visited the php manual to see how to collect the data from your resultset? – mickmackusa Apr 20 '18 at 13:17
  • What's the exact error message you get? And @mickmackusa is correct that `mysql_` functions are very much no longer recommended. – TRiG Apr 20 '18 at 13:19
  • nearly... https://stackoverflow.com/questions/42390548/php7-produce-error-when-array-push-is-used-on-a-string I'll keep looking – mickmackusa Apr 20 '18 at 13:23
  • @ThisGuyHasTwoThumbs it's "[] operator not supported for strings" – Dormilich Apr 20 '18 at 13:26
  • The actual error is given in the title - [] operator not supported for strings – Nodoid Apr 20 '18 at 13:26
  • https://stackoverflow.com/questions/5879675/problem-with-fatal-error-operator-not-supported-for-strings-in ? or https://stackoverflow.com/questions/5896665/operator-not-supported-for-strings ? or https://stackoverflow.com/q/12338263/2943403 ? – mickmackusa Apr 20 '18 at 13:27
  • @Dormilich ah my bad not actually seen this error before haha will rm comments :) – treyBake Apr 20 '18 at 13:28
  • Possible duplicate of [Php error when trying to output array](https://stackoverflow.com/questions/45884800/php-error-when-trying-to-output-array) – mickmackusa Apr 20 '18 at 13:30

1 Answers1

5

In $posts = array_shift($posts); you overwrite the array $posts with its first element - a string - and thus on the next loop cycle $posts[] = $post; emits said error.

Dormilich
  • 927
  • 5
  • 11