Databases Reference
In-Depth Information
+-----------+---------------------------+
| artist_id | artist_name |
+-----------+---------------------------+
| 1 | New Order |
| 2 | Nick Cave & The Bad Seeds |
| 3 | Miles Davis |
| 4 | The Rolling Stones |
| 5 | The Stone Roses |
| 6 | Kylie Minogue |
+-----------+---------------------------+
6 rows in set (0.00 sec)
Example 13-2 uses PHP to do the same thing: use the music database, run the SELECT
query, and display the results formatted in a table.
Example 13-2. Querying the music database with PHP
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html401/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1" />
<title>Artists</title>
</head>
<body>
<h1>Artists</h1>
<table>
<tr>
<th>artist_id</th>
<th>artist_name</th>
</tr>
<?php
// Connect to the MySQL server
if (!($connection = @ mysql_connect("localhost", "root",
" the_mysql_root_password ")))
die("Cannot connect");
if (!(mysql_select_db("music", $connection)))
die("Couldn't select music database");
// Run the query on the connection
if (!($result = @ mysql_query("SELECT * FROM artist", $connection)))
die("Couldn't run query");
// Until there are no rows in the result set, fetch a row into
// the $row array and ...
while ($row = @ mysql_fetch_array($result, MYSQL_ASSOC))
{
// Start a table row
print "<tr>\n";
 
Search WWH ::




Custom Search