【问题标题】:storing the mysqli result into array and then printing it with a while loop将 mysqli 结果存储到数组中,然后使用 while 循环打印它
【发布时间】:2015-04-19 04:29:11
【问题描述】:

你好,我正在尝试将 mysqli 查询的结果存储到一个数组中,然后将该数组存储到会话中,然后在 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 循环存储到会话中的数组 然后我需要打印为

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

【问题讨论】:

    标签: php mysql arrays session mysqli


    【解决方案1】:

    您已将行从 mysqli_result 转换为 array,因此您需要遍历数组然后将其打印出来,这样应该可以:

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

    【讨论】:

    • 啊.. 我认为这将取决于您何时运行代码,第一次运行代码时$GEttheBIrthdaYS 的类型为mysqli_result,并将get 作为@987654326 放入会话中@。然后第二次运行代码时,数据在会话中,此时$GEttheBIrthdaYS 现在是一个数组。我的代码应该可以用于第二次运行。
    • 我已经让它在第一回合运行了 .. :p 感谢它成功了... :)
    • 很高兴你把它整理好了! :)
    【解决方案2】:

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

    把它放在你的 mysqli_query() 之后看看发生了什么。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 2010-10-12
      • 2013-11-30
      • 1970-01-01
      • 2022-06-28
      • 2018-07-02
      相关资源
      最近更新 更多