dongyipa0028 2014-01-06 07:19
浏览 20
已采纳

努力将SQL获取的结果存储在PHP中的数组中

I am struggling to store the sql fetched results in an array. I tried alot of suggested methods I found on the internet and I cant get it to work. I am obviously doing something wrong. This is the latest code I am testing atm:

// The following code is place after prepare() and mysqli_stmt_execute($stmt)

    mysqli_stmt_bind_result($stmt, $colDescription);

    $descArray = array();
    $i = 0;

    /* fetch values and store them to each variables */
    while (mysqli_stmt_fetch($stmt)) {          

        $fetch = $colDescription;
        $descArray[$i] = $fetch;
        $i++;
    }

    //For testing to see what echos in that array   
    foreach($descArray as $v) { 
        echo $v;
    }

In the above code, when I echo the array, only the last row of the table is outputted. So I think I am basically overlapping the values to the last row and its not really storing all the fetched values in an array. What am I doing wrong here? Can someone guide me please?

  • 写回答

2条回答 默认 最新

  • dongzi0850 2014-01-06 16:13
    关注

    I couldnt quite get it to work with mysqli after trying for almost 2 days on this. So I have switched to PDO and I found that was so much easier to deal with array results that are fetched. This is what I have used now in the PDO method and it works like a charm:

    // The following code is place after PDO prepare() and execute()
    
        foreach ($stmt as $row) {
            // do something with $row
    
                $description[] = $row['description'];
        }
    

    You can see how easy it was and it just took a couple of lines to do this. It was like a nightmare when I was trying to achieve the same with mysqli and I dont even know if it was going to be possible. I tried so many methods with no luck. Even if it was possible with mysqli I am quite sure it would have taken a long windy and dirty looking code just to achieve something simple like this. So its totally worth yet to make the switch to PDO.

    I hope this helps to someone stuck on this like me...

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

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示