dongli8466 2012-05-17 08:20
浏览 34
已采纳

PHP:从SQL查询中读取数据到数组中

I'm newbie in PHP. I wrote the below-given code to create the array "data". It should have 10 rows, BUT for some reason only last (10th) row is filled. The SQL query is fine for sure, because I checked it in MySQL Query Manager (the query returns 10 rows).

$query1="SELECT * FROM tab1, tab2 WHERE tab1.column1=tab2.column2;";
    $result1=DatabaseConnector::ExecuteQueryArray($query1);
    $data = array();
    $i = 0;
    foreach ($result1 as $row):
        $data = array(
            array($i,array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00"));
        $i++;
    endforeach;

Update 1: I have another related to my initial question. When I try reading data from the array, the error "Undefined offset: 1" occurs. The funny thing is that when I filled "data" array using $data = and not $data[] =, there was no error, just the last row was filled.

for($i=0; $i<count($data); ++$i) {
        $bar = new GanttBar($data[$i][0],$data[$i][1],$data[$i][2],$data[$i][3]);
        $graph->Add($bar);
    }
  • 写回答

3条回答 默认 最新

  • doudu6100 2012-05-17 08:22
    关注

    That's because you're overriding the entire $data variable on each iteration. Use

    $data[] = array(...
    

    As for the newest error - You should be using foreach to iterate an array. That's what it was made for.

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

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?