mercredi 25 juin 2014

Store the database table to array php


Vote count:

0




Please someone help me. I have problem like this:


I want to get data from database and store it in array. Example:


my product table:


id::name::price::qty


1::Red Shoes::10::2


2::Black Dress::20::3


3::Blue Skirt::30::1


I want get data from that table an store in array like this:



$items = [
array(
'id' => '1',
'price' => 10,
'quantity' => 2,
'name' => 'Red Shoes'
),
array(
'id'=> '2',
'price' => 20,
'quantity' => 3,
'name' => 'Black Dress'
),
array(
'id'=> '3',
'price' => 30,
'quantity' => 1,
'name' => 'Blue Skirt'
)
];


I try using this code:



$query = mysql_query("SELECT * FROM product");
$data_item = array();

while ($row = mysql_fetch_array($query)) {

$data_item['id'] = $row['id'];
$data_item['price'] = $row['price'];
$data_item['quantity'] = $row['qty'];
$data_item['name'] = $row['name'];

}

$items = [$data_item];

print_r($items);


This is the output:



Array ( [0] => Array ( [id] => 3 [price] => 30 [quantity] => 1 [name] => Blue Skirt ) )


With that code I just get the last data and 2 others not store inside array. Why this happen?


How to get output like this:



Array ( [0] => Array ( [id] => 1 [price] => 10 [quantity] => 2 [name] => Red Shoes ) [1] => Array ( [id] => 2 [price] => 20 [quantity] => 3 [name] => Black Dress ) [2] => Array ( [id] => 3 [price] => 30 [quantity] => 1 [name] => Blue Skirt ) )


Can someone help me please?



asked 38 secs ago






Aucun commentaire:

Enregistrer un commentaire