Hello again,
I don't think I explained what I did well enough so here's another go at it.
Quote:
quote:
What if you have fields with the same name in the two tables.
|
As a general rule of thumb whenever possible and within your control avoid using the same field names in different tables unless that data is expected to be related, as is the case with the 'user_id' field in my example. It hadn't crossed my mind that you might be using fields with the same name and the data *not* be related. But again, the foreach there will help you to determine how associative indices are named. Otherwise if the second argument of in this case, the mysql_fetch_array function can be specified to use numeric, associative or both types of indices. Meaning you may access the data by the string representation of the field, or the numeric order with which it appears in the query.
http://www.php.net/mysql_fetch_array
So in my example I called on user_ids.user_id, user_name (from the user_id table) user_profiles.user_id (Expected to contain related data), and first_name (from the user_profiles table). As long as each field is unique there is no need to specifiy the table before it.
Here is another test script to see how the query handles data if the fields are unrelated. In this example I changed the name of the first_name field to user_name.
Code:
$result = mysql_query("SELECT `user_ids`.`user_id`, `user_ids`.`user_name`, `user_profiles`.`user_id`, `user_profiles`.`user_name` FROM `user_ids`, `user_profiles` WHERE `user_ids`.`user_id` = `user_profiles`.`user_id`");
if (empty($result))
{
echo mysql_error().": ";
echo mysql_errno()."<br /><br />\n";
}
echo "<br />";
$data = mysql_fetch_array($result);
foreach($data as $key => $value)
{
echo "\$data[\"$key\"] = $value;<br />\n";
}
Outputs:
$data["0"] =
user_ids.user_id;
$data["user_id"] =
user_ids.user_id;
$data["1"] =
user_ids.user_name;
$data["user_name"] =
user_ids.user_name;
$data["2"] =
user_profiles.user_id;
$data["3"] =
user_profiles.user_name;
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::