php 从mysql表中获取行到php数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7491768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:47:43  来源:igfitidea点击:

Get rows from mysql table to php arrays

phpmysqlarrays

提问by redfrogsbinary

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.

如何获取mysql表的每一行并将其放入php数组中?我需要一个多维数组吗?所有这些的目的是稍后在谷歌地图上显示一些点。

回答by Mr. Starburst

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.

此行进入您的表并从您的表中获取“someFieldName”中的数据。如果您想获得多个列,您可以在 'someFieldName' 中添加更多字段名称。

$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!).

因此,如果 $theRowYouWant 等于 4,它将是第 5 行的数据(在本例中为“someFieldName”)(记住,行从 0 开始!)。

回答by Marc B

$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.

回答by GregM

The other answers do work - however OP asked for all rows and if ALL fields are wanted as well it would much nicer to leave it generic instead of having to update the php when the database changes

其他答案确实有效 - 但是 OP 要求所有行,如果还需要所有字段,最好让它保持通用,而不必在数据库更改时更新 php

$query="SELECT * FROM table_name";

Also to this point returning the data can be left generic too - I really like the JSON format as it will dynamically update, and can be easily extracted from any source.

到目前为止,返回数据也可以保持通用 - 我真的很喜欢 JSON 格式,因为它会动态更新,并且可以轻松地从任何来源提取。

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
{
  echo json_encode($row);
}

回答by Surendra Narayan Roy

HERE IS YOUR CODE, USE IT. IT IS TESTED.

这是您的代码,请使用它。它已经过测试。

$select=" YOUR SQL QUERY GOOES HERE";
$queryResult= mysql_query($select);

//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();

//STORE ALL THE RECORD SETS IN THAT ARRAY 
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) 
{
    array_push($data_array,$row);
}


mysql_free_result($queryResult);


//TEST TO SEE THE RESULT OF THE ARRAY 
echo '<pre>';
print_r($data_array);
echo '</pre>';

THANKS

谢谢

回答by Adam

You can do it without a loop. Just use the fetch_allcommand

你可以在没有循环的情况下做到这一点。只需使用fetch_all命令

$sql     = 'SELECT someFieldName FROM yourTableName';
$result  = $db->query($sql);
$allRows = $result->fetch_all();