How to store row/column of mysql data in array

后端 未结 2 1759
孤街浪徒
孤街浪徒 2021-01-12 15:10

I want to be able to store (not echo) some data that has been selected from a mysql database in a php array. So far, I have only been able to echo the information, I just wa

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 15:56

    You could use

    $query = "SELECT interests FROM signup WHERE username = '".mysql_real_escape_string($username)."'";
    $result = mysql_query($query) or die ("no query");
    
    $result_array = array();
    while($row = mysql_fetch_assoc($result))
    {
        $result_array[] = $row;
    }
    

    This will basically store all of the data to the $result_array array.

    I've used mysql_fetch_assoc rather than mysql_fetch_array so that the values are mapped to their keys.

    I've also included mysql_real_escape_string for protection.

提交回复
热议问题