Get rows from mysql table to php arrays

前端 未结 5 2043
借酒劲吻你
借酒劲吻你 2021-02-04 10:10

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 ma

5条回答
  •  一生所求
    2021-02-04 10:43

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

提交回复
热议问题