-2

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?

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
catcher
  • 97
  • 2
  • 9
  • 2
    Syntax error:- `for($i=0;$i<$total_deger);$i++){` check the extra bracket? Is it a TYPO? – Alive to die - Anant Nov 18 '16 at 09:14
  • yes, It's ok now.. Thank you. But now my $no=1 and I have if statement $no<$total_deger. $total_deger is 23. But it looks like $no<$total_deger and my if statement is true. What is wrong? – catcher Nov 18 '16 at 09:20
  • 2
    Also `echo $xxx[i];` should be `echo $xxx[$i];` **Look at your PHP Error log when things dont work as expected** – RiggsFolly Nov 18 '16 at 09:20
  • 1
    That also applies to this line ALL the `i` variables need to be `$i` in `$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 ",";}` – RiggsFolly Nov 18 '16 at 09:23
  • 1
    Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Nov 18 '16 at 09:24
  • Ok. Thank you. I was C# person and now learning PHP. So I forget $ sign.Thanks you – catcher Nov 18 '16 at 09:29

2 Answers2

3

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']."]";
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
1

Firstly you want to check your SQL query returns rows. Then I think you need to modify your foreach to a while as below. Also checking that rows are returned before looping.

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        $data[] = $row;
    }
}

As per example given here: http://www.w3schools.com/php/php_mysql_select.asp

Shaun Parsons
  • 362
  • 1
  • 11