PHP Array name question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Developer101
Forum Newbie
Posts: 2
Joined: Mon Jul 21, 2003 2:18 am

PHP Array name question

Post 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']...";
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

leenoble_uk
Forum Contributor
Posts: 108
Joined: Fri May 03, 2002 10:33 am
Location: Cheshire
Contact:

AS

Post 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
Developer101
Forum Newbie
Posts: 2
Joined: Mon Jul 21, 2003 2:18 am

Post by Developer101 »

Ah, so trick is to use aliases. :) Now why didn't I think of that. Heh heh. Thanks.
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post 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;

}
Post Reply