Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing data from SQL in array

Tags:

arrays

sql

php

I am trying to store the data from my sql database into an array. Currently I have this:

$query = mysql_query("SELECT * FROM  `InspEmail` WHERE  `Company` LIKE  '$company'");

while($row = mysql_fetch_array($query))
    {

        $inspector = $row['name'];

    }

The problem is that I have 8 rows of data. I need to store each 8 names from that database into an array. When I try this:

$inspector = array($row['name']);

It doesn't work.

like image 699
Jason Avatar asked Jul 13 '26 00:07

Jason


1 Answers

If you want to store all of the names in an array, you need to define the array outside the scope of the while loop and append to it. Like this:

$nameArray = array();
while($row = mysql_fetch_array($query)) {
    // Append to the array
    $nameArray[] = $row['name'];   
}
like image 140
andrewmitchell Avatar answered Jul 15 '26 12:07

andrewmitchell