Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rows from mysql table to php arrays

Tags:

arrays

php

mysql

How can i get every row of a mysql table and put it in a php array? Do i need a multidimensional array for this? The purpose of all this is to display some points on a google map later on.

like image 530
redfrogsbinary Avatar asked Sep 20 '11 21:09

redfrogsbinary


People also ask

How fetch data from database in array in PHP?

Data can be fetched from MySQL tables by executing SQL SELECT statement through PHP function mysql_query. You have several options to fetch data from MySQL. The most frequently used option is to use function mysql_fetch_array(). This function returns row as an associative array, a numeric array, or both.

How can we store values to array from MySQL database in PHP?

You have 2 ways of doing it: You can create a table (or multiple tables linked together) with a field for each key of your array, and insert into each field the corresponding value of your array. This is the most common way. You can just have a table with one field and put in here your array serialized.

How fetch data from database in PHP and display in table?

php $connect=mysql_connect('localhost', 'root', 'password'); mysql_select_db("name"); //here u select the data you want to retrieve from the db $query="select * from tablename"; $result= mysql_query($query); //here you check to see if any data has been found and you define the width of the table If($result){ echo "< ...

What is $row in PHP?

Return Value: Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set. PHP Version: 5+


2 Answers

You need to get all the data that you want from the table. Something like this would work:

$SQLCommand = "SELECT someFieldName FROM yourTableName";

This line goes into your table and gets the data in 'someFieldName' from your table. You can add more field names where 'someFieldName' if you want to get more than one column.

$result = mysql_query($SQLCommand); // This line executes the MySQL query that you typed above

$yourArray = array(); // make a new array to hold all your data


$index = 0;
while($row = mysql_fetch_assoc($result)){ // loop to store the data in an associative array.
     $yourArray[$index] = $row;
     $index++;
}

The above loop goes through each row and stores it as an element in the new array you had made. Then you can do whatever you want with that info, like print it out to the screen:

echo $row[theRowYouWant][someFieldName];

So if $theRowYouWant is equal to 4, it would be the data(in this case, 'someFieldName') from the 5th row(remember, rows start at 0!).

like image 164
Mr. Starburst Avatar answered Oct 18 '22 09:10

Mr. Starburst


$sql = "SELECT field1, field2, field3, .... FROM sometable";
$result = mysql_query($sql) or die(mysql_error());

$array = array();

while($row = mysql_fetch_assoc($result)) {
   $array[] = $row;
}

echo $array[1]['field2']; // display field2 value from 2nd row of result set.
like image 31
Marc B Avatar answered Oct 18 '22 07:10

Marc B