mysqli fetch_array()只将检索到的数据的第一行转换为数组

时间:2014-11-29 04:51:41

标签: php arrays mysqli fetch

似乎正在发生的是 $ result_tag 只保存检索到的第一行sql数据。

当我在SQl上运行结果查询时返回一个包含多行的表。 但是,当我 var_dump()时,它只返回第一行而没有别的。

  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

2 个答案:

答案 0 :(得分:1)

更改为

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

答案 1 :(得分:1)

您想使用fetch_all(); fetch_array()只返回一行(作为数组) 见http://php.net/manual/en/mysqli-result.fetch-all.php

这实际上是最外面的事情,用fetch_array()

一次取一行