I have the following PHP code that selects all values in a MySQL database:
<?php
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
?>
How can I assign what the query returns to a variable so that I can echo the results out for each row?
mysql_query returns a resource you can use with these methods to fetch each row from the result:
The most common function used is mysql_fetch_assoc which fetches the row as an associative array using the column names as the key names:
I would recommend switching to the MySQLi extension instead. It supports more MySQL features and has procedural and object-oriented implementations. The PDO_MYSQL extension offers some improvements over MySQLi, but it only has an object-orientated implementation so it takes a little more to get up and running with.