Page 1 of 1

PHP Array name question

Posted: Mon Jul 21, 2003 2:18 am
by Developer101
This should be an easy question to answer. :D

I understand how to use variable names in my array statements:

Code: Select all

<?php
.
.
.
$query = mysql_query("SELECT Name FROM Users WHERE ID=1");
while($Record=mysql_fetch_array($query))
&#123;
        echo "Username: ".$Record&#1111;"Name"]."<br>";
&#125;
.
.
.
?>
I just started using joins in my SQL statements. So now I am faced with a weird situation. Look at the next example:

Code: Select all

<?php
.
.
.
$query = mysql_query("SELECT Users.Name, Games.Name FROM Users INNER JOIN Games ON (Users.GameID = Games.ID) WHERE Users.ID=1");
while($Record=mysql_fetch_array($query))
&#123;
        // Here is the question. $Record&#1111;"Users.Name"] won't work.
        // $Record&#1111;"Name"] won't work because there are 2 of them in the record set.
        // I could just use $Record&#1111;0] and $Record&#1111;1], but it's kind of sloppy and error prone if I change the SQL statement.
        // How do I acces these by name with this kind of query?
&#125;
.
.
.
?>
Oh, and one other question while I am at it. When using record set arrays like above, do I always have to imbed them like:

Code: Select all

echo "This users name is ".$Record&#1111;"Name"]."...";
Or is there a way to imbed them like normal strings (this won't work):

Code: Select all

echo "This users name is $Record&#1111;"Name"]...";
Hmmm, don't think I have tried this before. Maybe this is the trick:

Code: Select all

echo "This users name is $Record&#1111;'Name']...";

Posted: Mon Jul 21, 2003 3:12 am
by twigletmac

AS

Posted: Mon Jul 21, 2003 9:33 am
by leenoble_uk
You could always use AS in your mysql code..

Code: Select all

SELECT table1.name AS name_one, table2.name AS name_two FROM USER"
then refer to the 'AS' name

$result["name_one"];

BINGO

Posted: Mon Jul 21, 2003 3:51 pm
by Developer101
Ah, so trick is to use aliases. :) Now why didn't I think of that. Heh heh. Thanks.

Posted: Mon Jul 21, 2003 4:02 pm
by xisle
aliases are goood, you can also use the list function and ignore the duplicate fields.

while(list($field1, , $field3)=mysql_fetch_array($query)) {

echo "This users name is".$field1;

}