Using an SQL result in a foreach loop

后端 未结 5 1593
谎友^
谎友^ 2021-01-06 10:36

I feel like I am missing something stupidly obvious here, I am trying to get the results of an SQL query and then using them in a loop. I feel like I am missing something st

5条回答
  •  一个人的身影
    2021-01-06 11:00

    You must first fetch your results into an array. Looks like you started to do this but commented it out.

    $results = mysql_query($query);
    //$userData = mysql_fetch_array($results, MYSQL_ASSOC);
    
    $resultset = array();
    while ($row = mysql_fetch_array($results)) {
      $resultset[] = $row;
    }
    
    // $resultset now holds all rows from the first query.
    foreach ($resultset as $result){
     //... etc...
    

提交回复
热议问题