Databases Reference
In-Depth Information
Distribution and replication functions
Many new functions have been added to allow you to manage many MySQL servers
that perform the same tasks. This allows you to build highly scalable systems that
can handle hundreds of thousands or millions of requests each day.
We don't discuss these features in detail as they're outside the scope of this topic.
Accessing Query Results with mysql_fetch_array(
) and
mysqli_fetch_array( )
The
mysql_fetch_array( )
and
mysqli_fetch_array( )
functions retrieve result rows
from queries that produce output. These functions are typically used to retrieve the
results output by an SQL
SELECT
statement. This section uses examples to show how.
To keep our description simple, we'll use the standard MySQL library, but our ex-
planations apply to
mysqli_fetch_array( )
as well.
As with the array examples described previously, you can access elements returned with
mysql_fetch_array( )
using either
numeric
or
associative
access. For numeric access,
attributes are numbered in the order they are specified in the SQL statement. If you use
a
SELECT * FROM
table
statement, then the attributes are ordered first by the
table
names and then by the order they were created with the
CREATE TABLE
statement (which
is as listed by the output of the
DESCRIBE
or
SHOW
statements). Let's explore three ex-
amples of numeric access that illustrate these ideas.
We'll begin with a simple example that uses the
music
database. Suppose you want to
output the
artist_id
column and then the
artist_name
column for all rows from the
artist
table. You can do this with the following PHP fragment:
// Tell the browser to expect preformatted text
print "<pre>";
// Run the query on the connection
if (!($result = @ mysql_query("SELECT artist_id, artist_name FROM artist",
$connection)))
die("Couldn't run query");
// Until there are no rows in the result set, fetch a row into
// the $row array.
while ($row = @ mysql_fetch_array($result))
{
// Start a new line
print "\n";
// Print out the columns
print "{$row[0]} {$row[1]}";
}
// Tell the browser that the preformatted text has ended
print "</pre>";