开发者

How can I convert record set from database to array?

开发者 https://www.devze.com 2023-04-03 21:56 出处:网络
How can I convert record set from database to array? It has 1 table named: tblproduct, with the following schema:

How can I convert record set from database to array? It has 1 table named: tblproduct, with the following schema:

 product_id | product_name | product_desc | product_price | product_img
开发者_开发知识库

When I select data from the database I would like an array in the following format:

$product_array = array(
  "105" => array('product_id' => '105', 'product_name' => 'Blackberry 8900',
                 'product_desc' => '', 'product_price' => '1150.00',
                 'product_img' => 'products/product5.jpg'),
  "106" => array('product_id' => '106', 'product_name' => 'Headphone with mic',
                 'product_desc' => '', 'product_price'=>'148.85', 
                 'product_img' => 'products/product8.jpg')
);

Best regards


Assuming you are using MySQL (You didn't specify your DBMS) and have a line like:

$result = mysql_query('SELECT * FROM `tblproduct`');

You can use:

while($row = mysql_fetch_assoc($result))
    $product_array[$row['product_id']] = $row;
0

精彩评论

暂无评论...
验证码 换一张
取 消