Get rows from mysql table to php arrays

前端 未结 5 2041
借酒劲吻你
借酒劲吻你 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:20

    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

    0 讨论(0)
  • 2021-02-04 10:23

    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

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

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) 
    {
      echo json_encode($row);
    }
    
    0 讨论(0)
  • 2021-02-04 10:33

    You can do it without a loop. Just use the fetch_all command

    $sql     = 'SELECT someFieldName FROM yourTableName';
    $result  = $db->query($sql);
    $allRows = $result->fetch_all();
    
    0 讨论(0)
  • 2021-02-04 10:34
    $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.
    
    0 讨论(0)
  • 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!).

    0 讨论(0)
提交回复
热议问题