将mysqli结果存储到数组中,然后使用while循环打印它

时间:2015-04-19 04:29:12

标签: php mysql arrays session mysqli

你好我试图将mysqli查询的结果存储到一个数组中,然后将数组存储到session中,然后在其他页面上使用mysqli_fetch_array的帮助打印会话。

我的代码看起来像......

      $arraysession=array();
  if(!isset($_SESSION['query_result'])){
 $GEttheBIrthdaYS=mysqli_query($conn,"select something from tablename where (something=something1) order by id desc limit 40");
 while($res = mysqli_fetch_row($GEttheBIrthdaYS)){
    array_push($arraysession, $res);
 }$_SESSION['query_result']=$arraysession;
 } else {
 $GEttheBIrthdaYS = $_SESSION['query_result'];
}
 while($data=mysqli_fetch_array($GEttheBIrthdaYS)){
  }

数组存储在会话中,我甚至可以通过

打印数组
echo '<pre>',print_r($GEttheBIrthdaYS,1),'</pre>';

当我打印数组时,它就像

    Array
  (
  [0] => Array
    (
        [0] => 160
        [1] => name1
        [2] => image
        [3] => 1995-05-28 00:00:00
    )

[1] => Array
    (
        [0] => 158
        [1] => name2
        [2] => image.jpg
        [3] => 1994-06-14 00:00:00
    )

[2] => Array
    (
        [0] => 157
        [1] => name3
        [2] => image.jpg
        [3] => 1995-01-15 00:00:00
    )

[3] => Array
    (
        [0] => 155
        [1] => name4
        [2] => image.jpg
        [3] => 1993-08-26 00:00:00
    )

[4] => Array
    (
        [0] => 154
        [1] => name5
        [2] => image.gif
        [3] => 1993-09-27 00:00:00
    )

   )

问题是当我用mysqli_fetch_array打印数组时它显示

  mysqli_fetch_array() expects parameter 1 to be mysqli_result, array given in f/:something/directory/page on line 3

我只是想知道如何通过while循环打印存储到session中的这个数组 我需要打印然后

 while($data=mysqli_fetch_array($GEttheBIrthdaYS)){
 echo $data['dob']; ..........     
 }

2 个答案:

答案 0 :(得分:1)

您已将行从mysqli_result转换为array,因此您需要对数组进行迭代,然后将其打印出来,这样的事情应该有效:

foreach($GEttheBIrthdaYS as $data) {
    echo $data[0];
    echo '<br>'.$data[1];
    echo '<br>'.$data[2];
    echo '<br>'.$data[3];
}

答案 1 :(得分:1)

您的查询可能失败并返回错误值。

将这个放在你的mysqli_query()之后,看看是怎么回事。

if (!$$GEttheBIrthdaYS) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
}