I hope to get some help on a problem that I suppose must be simple but I have been stuck for a while.
I have a simple Mysql table with 3 fields ( id, customer, sales ) . All I need is to query this table and get an php array.
Here's my code:
$query = "SELECT * FROM mysqltable";
$result = $mysqli->query($query))
I need to write a code to loop and get an array in this format:
$row=array(0=>"500,200",1=>"500,300",2=>"1000,600",3=>"800,400",4=>"200,500");
So I can end with
echo json_encode($row);
Would appreciate any help to solve this.
You can use mysqli_fetch_all()
mixed mysqli_result::fetch_all ([ int $resulttype = MYSQLI_NUM ] )
or
mixed mysqli_fetch_all ( mysqli_result $result [, int $resulttype = MYSQLI_NUM ] )
for reference read http://php.net/manual/en/mysqli-result.fetch-all.php
also there is
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);
while($row = $result->fetch_array())
{
$rows[] = $row;
}
foreach($rows as $row)
{
echo $row['CountryCode'];
}
/* free result set */
$result->close();
/* close connection */
$mysqli->close();
?>