Kind of new to PHP, have moved from ASP.
My question is I am trying to put the results of a query on a mySQL database into a multi dimensional array but having no success!
My Code.....
<?php
require ('conn.php');
$MYSQL = mysql_query("SELECT * FROM Cost order by CostID Asc");
$RS = mysql_fetch_array($MYSQL,MYSQL_ASSOC);
$i = 0;
while ($RS = mysql_fetch_array($MYSQL,MYSQL_ASSOC))
{
$CostArray = array(
i => array($RS{'CostID'},$RS{'CostName'},$RS{'CostType'},$RS{'CostPrice'})
);
$i++;
}
print_r($CostArray);
?>
I am trying to create an array so I can call each element as:
$CostArray[0][0]
$CostArray[0][1] etc...
Please help.
Tx
Multi Dimensional Arrays
Moderator: General Moderators
if you want it stored as numericals, after the query do
$mydata = array();
while ($row = mysql_fetch_row($RS)) $mydata[] = $row;
then access it as you indicated.
I find it 'prettier' to use associative naming, use this
$mydata = array();
while ($row = mysql_fetch_assoc($RS)) $mydata[] = $row;
and access with
$mydata[rownumber]['columnname']
e.g. $mydata[4]['CostId'] for the CostId value of record 5
$mydata = array();
while ($row = mysql_fetch_row($RS)) $mydata[] = $row;
then access it as you indicated.
I find it 'prettier' to use associative naming, use this
$mydata = array();
while ($row = mysql_fetch_assoc($RS)) $mydata[] = $row;
and access with
$mydata[rownumber]['columnname']
e.g. $mydata[4]['CostId'] for the CostId value of record 5