Harold Finch
Harold Finch

Reputation: 586

How to save all query results into an array

I'm trying to store the query results into an array. I've a table with 50 rows and 6 columns, how to save this 50 values into an array? My problem is save into array like:

Value1 - red - metal - 100 - in stock - price

So each cell of array must have this organization.

Upvotes: 0

Views: 1402

Answers (4)

rahulinaction
rahulinaction

Reputation: 354

You can use 2 dimensional array in this manner

 $sql = "select * from table";
    $result = mysql_query($query); 
    $myArray = array();
    while(*emphasized text*$arrayresult = mysql_fetch_array($result)) {
       $myArray[] = array(
                        'id'=>$arrayresult['id'],
                        'title'=>$arrayresult['title']
                    );
    }

Upvotes: 1

gsanthosh91
gsanthosh91

Reputation: 371

MySQL CONCAT() function is used to add two or more strings in query

Example:

SELECT CONCAT(pub_city,'--> ',country)
FROM publisher;

Upvotes: 0

Vishal Wadhawan
Vishal Wadhawan

Reputation: 1085

you can fetch it using concat from the database itself

  $query = "select concat(id,' - ',first_name,' - ' ,last_name) from table_name";

Upvotes: 0

Harikrishnan
Harikrishnan

Reputation: 9979

Use 2 dimensional array.

You may use like

$multi_array = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    array_push($multi_array, $row);
}

Upvotes: 0

Related Questions