Databases Reference
In-Depth Information
// the $row array.
while ($row = @ mysql_fetch_array($result))
{
print "\n";
// Print out the columns
print "{$row["id1"]} {$row["id2"]}";
}
You can see that the columns are accessed in the $row array using their aliases, id1 and
id2 .
You can't solve the name-clash problem using table or database names as prefixes. For
example, suppose you run the following query:
if (!($result = @ mysql_query(
"SELECT artist.artist_id,
album.artist_id
FROM artist, album
WHERE artist.artist_id = album.artist_id",
$connection)))
die("Couldn't run query");
If you try to access the column with the table name:
print "{$row["artist.artist_id"]}";
you'll get a PHP notice telling you that you're using an undefined index. If you omit
the table name, like this:
print "{$row["artist_id"]}";
you'll see the value for the last artist_id returned by the query, which is
album.artist_id . The best solution is to use aliases; you could design tables that avoid
duplicate names for columns you want to retrieve, but this could lead to a more con-
voluted design that's less clear and could lead to other problems.
The array returned by mysql_fetch_array( ) contains two elements for each column,
one each for numeric and associative access. You can see this when you use the fore
ach statement to output data:
// Run the query on the connection
if (!($result = @ mysql_query(
"SELECT * FROM artist, album
WHERE artist.artist_id = album.artist_id",
$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))
{
print "\n";
// Print out the columns
foreach($row as $element)
 
Search WWH ::




Custom Search