简体   繁体   中英

how to create associative array from sql table

this is my code

include('connect.php');
$prod = mysql_query("SELECT * FROM products");
$all = mysql_fetch_array($prod);

and i wont create associative array look like this

$product_array = array(
   '1' =>array('product_id'=>$all['pid'], 'product_name'=>$all['pname'],'product_desc'=>$all['pdesc'], 'product_price'=>$all['pprice'], 'product_img'=>$all['pimage']),
);

can you help me ? thanks !

If you want to get all the results from the query, you need to call mysql_fetch_array in a loop and push each row onto the array.

$product_array = array();
while ($all = mysql_fetch_array($prod)) {
    $product_array[] = array('product_id'=>$all['pid'], 'product_name'=>$all['pname'],'product_desc'=>$all['pdesc'], 'product_price'=>$all['pprice'], 'product_img'=>$all['pimage']);
}

Don't use mysql_* its deprecated, so use mysqli_* or PDO . Given example here:-

<?php
error_reporting(E_ALL); // check all errors
ini_set('display_errors',1); // display errors
$conn = mysqli_conncet('host name','user name','password','database name');//database connection code
$product_data = array(); // create an empty array
if($conn){
 $prod = mysqli_query($conn,"SELECT * FROM products");
    while ($all = mysql_fetch_array($prod)) {
        $product_data[] = array('product_id'=>$all['pid'], 'product_name'=>$all['pname'],'product_desc'=>$all['pdesc'], 'product_price'=>$all['pprice'], 'product_img'=>$all['pimage']); // assignment
    }
    echo "<pre/>";print_r($product_data); // print product_data array
}else{

  echo mysqli_connect_error(); // show what problem occur in database connectivity
}
mysqli_close($conn); // close connection
?>

you can call mysql_fetch_assoc() function to do it

include('connect.php');
$sql = mysql_query("SELECT product_id,
                            product_name,
                            product_desc,
                            product_price,
                            product_img
                      FROM products");
$prod = mysql_query($sql);
while ($r = mysql_fetch_assoc($prod)){
    $product_array[] = $r;
}

Use the below code for your desired output:

<?php
include('connect.php');
$prod = mysql_query("SELECT * FROM products");
$all = mysql_fetch_array($prod);
$allCount   =   count($all);
$product_array  =   array();

if($allCount)
{
    for($i=0; $i<$allCount; $i++)
    {
      $product_array[$i]['product_id']   =   $all[$i]['pid'];
      $product_array[$i]['product_name']  =   $all[$i]['pname'];
      $product_array[$i]['product_desc']  =   $all[$i]['pdesc'];
      $product_array[$i]['product_price'] =   $all[$i]['pprice'];
      $product_array[$i]['product_img']   =   $all[$i]['pimage'];   
    }
}

echo "<pre>";
print_r($product_array);
echo "</pre>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM