Databases Reference
In-Depth Information
</tr>
</table>
</body>
</html>
We get the output, but it's HTML code that's not very useful from the command line.
If you want to run PHP scripts from the command line, you don't need to include
HTML tags in the output, but you should make good use of the \n newline character.
Example 14-6 rewrites Example 14-3 without the HTML tags.
Example 14-6. Querying the music database from the command line
<?php
print "Artists\n=======\n";
printf("%-40s %-40s\n".
"---------------------------------------- ".
"----------------------------------------\n",
"artist_id", "artist_name");
// Connect to the MySQL server
if (!($connection = @ mysqli_connect("localhost", "root",
"")))
die("Cannot connect");
if (!(mysqli_select_db($connection, "music")))
die("Couldn't select music database");
// Run the query on the connection
if (!($result = @ mysqli_query($connection, "SELECT * FROM artist")))
die("Couldn't run query");
// Until there are no rows in the result set, fetch a row into
// the $row array and ...
while ($row = @ mysqli_fetch_array($result, MYSQL_ASSOC))
{
// print out each of the columns
foreach($row as $data)
printf("%-40s ", $data);
// Start a new line
print "\n";
}
?>
This will produce output as shown below:
Artists
=======
artist_id artist_name
---------------------------------------- ----------------------------------------
1 New Order
2 Nick Cave & The Bad Seeds
3 Miles Davis
4 The Rolling Stones
 
Search WWH ::




Custom Search