seamus
seamus

Reputation: 2901

mysqli fetch_array() is only turning first row of retrieved data into an array

What seems to be happening is $result_tag is only holding the first row of the sql data retrieved.

When I run the result query on SQl in returns a table with multiple rows. However, when I var_dump() it, it only returns the first row and nothing else.

  while($row = $results->fetch_array(MYSQLI_BOTH)) {    
  echo ....stuff that dont matter

  //Now I want some SQL results based on a result from ^^^ $row['ID']
  $result = $conn->query("SELECT tags.Tag FROM tags WHERE tags.results_ID =".$row['ID'] );
  $result_tag = $result->fetch_array(MYSQLI_NUM); 

  //I got the results. Now I want to compare them to another array '$explode' 
  //and echo out elements that are the same

    foreach($explode as $explodeValue){
      foreach($result_tag as $tag){
        if($tag == $explodeValue){
          echo $explodeValue;
        }
      }
    }

}//end of 1st while

Upvotes: 0

Views: 980

Answers (2)

Andras
Andras

Reputation: 3055

You want to use fetch_all(); fetch_array() returns just one row (as an array) See http://php.net/manual/en/mysqli-result.fetch-all.php

This is in fact what the outermost while is doing, fetching one row at a time with fetch_array()

Upvotes: 1

Rafael
Rafael

Reputation: 7746

Change to

$result = $conn->query("SELECT * FROM tags WHERE results_ID =".$row['ID'] );

Upvotes: 1

Related Questions