Databases Reference
In-Depth Information
mend that you use the new library for new code, but you should also learn about the
older library because you're likely to encounter it as you develop and modify PHP code.
The Original PHP MySQL Library
This section describes the original PHP library designed for MySQL versions earlier
than 4.1. In most PHP installations, it works with later versions, although you can't
take advantage of some newer MySQL features.
In Chapter 13, we showed you a simple PHP code example that uses the original MySQL
library. It's reproduced in Example 14-2.
Example 14-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