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

8
  • 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? Commented Apr 20, 2018 at 13:17
  • What's the exact error message you get? And @mickmackusa is correct that mysql_ functions are very much no longer recommended. Commented Apr 20, 2018 at 13:19
  • nearly... stackoverflow.com/questions/42390548/… I'll keep looking Commented Apr 20, 2018 at 13:23
  • @ThisGuyHasTwoThumbs it's "[] operator not supported for strings" Commented Apr 20, 2018 at 13:26
  • The actual error is given in the title - [] operator not supported for strings Commented Apr 20, 2018 at 13:26

1 Answer 1

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.

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

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.