MGamer333 MGamer333 - 1 month ago 5
MySQL Question

PHP MySQL content save to array sorted by id

$content_mysql = mysql_query("SELECT id, content FROM articles ");


I want to save content from mysql to an array, like:

// In the database table:

id: 1 content: oxigen

id: 2 content: hidrogen

//In the php array

echo $array['1']; //It is says: oxigen

echo $array['2']; //It is says: hidrogen

Answer
$content_mysql = mysql_query("SELECT id, content FROM articles ");

$array = [];

while ($row = mysql_fetch_assoc($content_mysql)) {
    $array[(string)$row['id']] = $row['content'];
}
Comments