Here I have a MySQL database and I need to retrieve many rows.
I need the data to stack up in one array:
$query_sel_node = "SELECT * FROM $table_name";
$result_data = mysql_query($query_sel_node);
$data_rec = array("data" => array(), "data_h" => array(), "file" => array(), "ptr" => array(), "name" => array());
while($data_fetched = mysql_fetch_assoc($result_data))
{
array_push($data_rec["data_h"], $data_fetched['data_h']);
array_push($data_rec["data"], $data_fetched['data']);
array_push($data_rec["file"], $data_fetched['m_file_thumb']);
array_push($data_rec["ptr"], $data_fetched['m_pointer']);
array_push($data_rec["name"], $data_fetched['m_name']);
}
now, i’ve added alot of columns to my table , which means i would need to array_push() for every single column i made.
is there a easier way that i can do this with fewer lines of code?
what i’m looking for is this:
i have a mysql table with column {m_data, m_name, m_file}
and i have several rows of data
now i need to get them into array
my array will use the column names an the key,
then i’ll store my row data into each key
First, get all of the column names from the desired table:
Build the array (
$table) with the column names and assign an empty array to each one (for the data):Fetch the data for each column and insert it into the
$tablearray:Now,
$tablewill contain the exact structure and data that the table$table_namedoes.