0

I am using a complex join statement to get data from my 'items' table where 'column1' is equal to the value of table2.ID

This is in a mysql_query() statement, and it should return 3 rows.

Now my understanding is that by using

$array=mysql_fetch_array($queryresult);

I can then loop through each 'row' using a

foreach($array as $output) {
     echo $output['ID'];
}

This is however not returning what i want. Using print_r on $output is outputting non-sensical information.

So, yes it is ver much back to basics, but obviously i have missed the point.

2
  • 1
    It's probably a problem with your MySQL syntax. What is the text of your query? Commented Sep 14, 2010 at 19:47
  • Are you sure the query is actually returning what you want? Have you tested it separately, via PHPMyAdmin/etc? Commented Sep 14, 2010 at 19:48

3 Answers 3

1

You need to use while loop:

while($row = mysql_fetch_array($queryresult)){
  // handle each row
}
Sign up to request clarification or add additional context in comments.

Comments

1

This is how I do it. This is by far not the end all solution... Just an example of how I do it.

$result = mysql_query($query, $dbconnect) or trigger_error("SQL", E_USER_ERROR);
     $i = 0;
     while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo $row["questionId"];
        echo $row["questionText"];
        echo $row["questionReview"];
     $i++;
     }  

http://php.net/manual/en/function.mysql-fetch-array.php

Comments

0

$array has a single row in it when you get to the loop, so when you say $output['ID'] you are one level deeper than you are expecting, walking through the columns instead of each row. When the ids don't exist or are translating to integers, thats where the nonsense comes in.

Use while($row = mysql_fetch_array($queryresult)) to walk through each row in the result set, then access the column values from $row['id'], $row['name'], etc. It will return false when there are no more rows.

The result will always be a single flat array with a single row per index id, regardless of the join dimensions.

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.