dpfwhb7470 2016-11-18 09:11
浏览 31
已采纳

从数据库MySQL制作数组

I have database connection. I want to make an array but something is problem i think. Here is my array code:

$var = "SELECT SUBSTRING(KayitTarihi,1,4) AS year,SUBSTRING(KayitTarihi,6,2) AS month,SUBSTRING(KayitTarihi,9,2) AS day,SUBSTRING(KayitTarihi,12,2) AS saat,SUBSTRING(KayitTarihi,15,2) AS dakika,Guc FROM Urun WHERE Date(KayitTarihi)=\"".$link_m."\"";

$result = $mysqli->query($var);

$data = array();
foreach ($result as $row) {$data[] = $row;}

print_r($data);

$no=1;$total_deger=count($data);
echo $total_deger;

for($i=0;$i<$total_deger);$i++){
    $xxx[i]="[Date.UTC(".$data[i]['year'].",".$data[i]['month'].",".$data[i]['day'].",".$data[i]['saat'].",".$data[i]['dakika'].",00),".$data[i]['Guc']."]";if($no < $total_deger){echo ",";}
    echo $xxx[i];
}

When I run this code, nothing happens in page. When I write to for example 0 for i. I can see my array value. Where do I mistake?

  • 写回答

2条回答 默认 最新

  • duan0530 2016-11-18 09:22
    关注

    Code with correction and suggestion (both are commented):-

    <?php
    error_reporting(E_ALL); // check all errors
    ini_set('display_errors',1);// display those errors
    $var = "SELECT SUBSTRING(KayitTarihi,1,4) AS year,SUBSTRING(KayitTarihi,6,2) AS month,SUBSTRING(KayitTarihi,9,2) AS day,SUBSTRING(KayitTarihi,12,2) AS saat,SUBSTRING(KayitTarihi,15,2) AS dakika,Guc FROM Urun WHERE Date(KayitTarihi)=\"".$link_m."\"";
    
    $result = $mysqli->query($var);
    
    $data = array();
    
    if ($result->num_rows > 0) { // check you got results or not
        while($row = $result->fetch_assoc()) { 
            $data[] = $row; // assign them
        }
    }
    
    //foreach ($result as $row) {$data[] = $row;} // not needed 
    
    print_r($data);
    
    $no=1;
    
    $total_deger= count($data);
    
    echo $total_deger;
    
    for($i=0;$i<$total_deger;$i++){ // remove )
    
    $xxx[$i]="[Date.UTC(".$data[$i]['year'].",".$data[$i]['month'].",".$data[$i]['day'].",".$data[$i]['saat'].",".$data[$i]['dakika'].",00),".$data[$i]['Guc']."]"; // use $i  instead of i
    if($no < $total_deger)
    {
        echo ",";
    }
    echo $xxx[$i];
    
    }
    

    Note:- Always add some error reporting code. So that all errors will populated and you can rectify those

    Also better would be to use foreach() instead of for loop (as it will take care of indexes itself):-

    //use foreach
    foreach ($data as $dat){
        echo "[Date.UTC(".$data['year'].",".$data['month'].",".$data['day'].",".$data['saat'].",".$data['dakika'].",00),".$data['Guc']."]";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 游戏盾如何溯源服务器真实ip?
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。
  • ¥50 win10链接MySQL
  • ¥15 抖音看过的视频,缓存在哪个文件