从mysql表获取行到php数组

red*_*ary 12 php mysql arrays

我怎样才能获得mysql表的每一行并将其放入php数组中?我需要一个多维数组吗?所有这一切的目的是稍后在谷歌地图上显示一些点.

Mr.*_*rst 31

您需要从表中获取所需的所有数据.像这样的东西会起作用:

$SQLCommand = "SELECT someFieldName FROM yourTableName";
Run Code Online (Sandbox Code Playgroud)

这一行进入你的表并从你的表中获取'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++;
}
Run Code Online (Sandbox Code Playgroud)

上面的循环遍历每一行并将其作为元素存储在您创建的新数组中.然后,您可以使用该信息执行任何操作,例如将其打印到屏幕上:

echo $row[theRowYouWant][someFieldName];
Run Code Online (Sandbox Code Playgroud)

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


Mar*_*c B 14

$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.
Run Code Online (Sandbox Code Playgroud)