duanpo2037 2010-04-23 16:24
浏览 7
已采纳

PHP数组问题......如果有人可以提供帮助!

Thanks for the contributions so far - I can see now that $rowq is not a single array but lots of them - I'd like to bring all the rows back in the array, any suggestions?

First off, the code which brings back my data into an array:

function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tques WHERE page = ?");
mysqli_stmt_bind_param($proc, "i", $page);
mysqli_stmt_execute($proc);


$rowq = array();
stmt_bind_assoc($proc, $rowq);

// loop through all result rows
while ($proc->fetch()) {
//      print_r($rowq);
}


mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rowq);
} 

Now, when I `print_r($rowq);' I get the following, which is all good:

Array ( [questions] => q1 [qnum] => 1 [qtext] => I find my job meaningful [page] => 1 ) Array ( [questions] => q2 [qnum] => 2 [qtext] => I find my job interesting [page] => 1 ) Array ( [questions] => q3 [qnum] => 3 [qtext] => My work supports ABC's objective [page] => 1 ) Array ( [questions] => q4 [qnum] => 4 [qtext] => I am able to balance my work and home life [page] => 1 ) Array ( [questions] => q5 [qnum] => 5 [qtext] => I am clear about what is expected of me in my job [page] => 1 ) Array ( [questions] => q6 [qnum] => 6 [qtext] => My induction helped me to settle into my job [page] => 1 ) Array ( [questions] => q7 [qnum] => 7 [qtext] => I understand the ABC vision [page] => 1 ) Array ( [questions] => q8 [qnum] => 8 [qtext] => I know how what I do fits into my team's objectives [page] => 1 )

Now, in my php page I have the following piece of script:

$questions = fetch_questions($page);

And when I print_r $questions, as below:

print_r($questions);

I only get the following back from the array, 1 row:

Array ( [questions] => q8 [qnum] => 8 [qtext] => I know how what I do fits into my team's objectives [page] => 1 )

Any ideas why that might be?

Thanks in advance,

Homer.

  • 写回答

2条回答 默认 最新

  • dpicx06888 2010-04-23 16:30
    关注

    Your $rowq variable is only holding the last row that was fetched. When you print_r during your fetch loop, each row is fetched into the $rowq variable, and then immediately printed, but because you overwrite the variable each iteration, when the loop completes only the final row is contained within $rowq.

    If you want to hang onto all the rows, you can augment your function:

    function fetch_questions($page) {
      // ...
    
      $rows = array();
      while ($proc->fetch()) {
        $rows[] = $rowq;
      }
    
      // ...
    
      return $rows;
    }
    

    With this code (only the relevant parts are included), every time a row is fetched, it is placed within an array $rows, which then contains all rows when the loop completes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上