473,503 Members | 8,131 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql_fetch_array and table aliases

I'm using mysql_fetch_array to run a query along these lines:

SELECT A.name, B.name FROM A, B WHERE A.id = B.id

I tried this code:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row['A.name'];
}

.... but the echo statement produces "Notice: Undefined index: A.name".
Of course, using just 'name' would be ambiguous because the query also
retrieves B.name (and my actual query will involve more tables and
probably select '*' [i.e. all columns], so it isn't trivial to use
'AS' to provide individual column aliases).

How can I access a column's value based on table *and* column name?

P.
Feb 9 '06 #1
4 3774
Paul E Collins wrote:
SELECT A.name, B.name FROM A, B WHERE A.id = B.id

I tried this code:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo $row['A.name'];
}

... but the echo statement produces "Notice: Undefined index: A.name".


1. (very bad) use $row[0] and $row['name']
2. (bad) use mysql_fetch_row(), $row[0], $row[1]. Doesn't work with
"select *".
3. (best) alias all duplicate column names using "as".

--
E. Dronkert
Feb 9 '06 #2
Of course when you use SELECT A.name, B.name blah blah you would get
the same column "name" as result (you can try this in console too), so
the best idea is using alias that representing the column like this :

SELECT
A.name AS name_A,
B.name AS name_B
FROM A, B WHERE A.id = B.id

and to access it just use $row['name_A'] ... the variable name still
gave me the information I need

PS: why don't you use LEFT JOIN ??

Feb 10 '06 #3
alvonsius wrote:
PS: why don't you use LEFT JOIN ??


Probably because it's different than his implicit INNER JOIN.

--
E. Dronkert
Feb 10 '06 #4
"alvonsius" <al**************@gmail.com> wrote:
SELECT
A.name AS name_A,
B.name AS name_B
FROM A, B WHERE A.id = B.id
Yep, thanks. The aliases make the query that little bit longer, but
it's not a big problem.
PS: why don't you use LEFT JOIN ??


Well, in my real query it's an inner join. This was just a rather
meaningless example. If you're asking why I use the comma notation, I
just find it a bit more readable than "JOIN... JOIN... JOIN" when
several tables are involved.

P.
Feb 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2879
by: Margaret MacDonald | last post by:
It appears that the 'table.field' dot notation is not preserved by php's mysql library. I can create some join 'SELECT a.id, b.id FROM table1 AS a, table2 AS b' but when I try to refer to the...
0
732
by: Andrew | last post by:
With command-line interface ( 3.23.37, UNIX Socket ) all is well with column aliasing. However, column aliases disappear in Excel, over ODBC, when there are multiple (joined) tables in the query. ...
4
12283
by: Chris Geihsler | last post by:
I have a set of udf's dealing that return a one column table of values parsed from a comma delimeted string. For example: CREATE FUNCTION . ( @patient_list varchar(2000) ) RETURNS...
16
3460
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
3
5140
by: rudodoo | last post by:
My company uses coldfusion to create web applications and we currently are using TOAD as a front end to creating SQL queries. I was wondering how is Access compared to TOAD on its ability to...
6
1411
by: Jeff Gardner | last post by:
Greetings: Is there a way to do <snip>SELECT table.* AS alias.*</snip>, or do I have to write additional code to automate the process? -- Regards, Jeff Gardner ___________________________
11
4715
by: chemlight | last post by:
I'm having a problem. I'm sure I'm going to kick myself over the answer... I have a table that stores vendors and their languages. This table starts out blank. I am querying the table to see if a...
1
1702
by: nmailey | last post by:
Hi all. Here's what I'm encountering in SQL 2000. I've got the following tables: program ------------ id (pk int) program_component_type_rel ------------
3
3272
by: mocha | last post by:
I have this error message --> Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\script.php on line 24 You have an error in your SQL syntax; check the...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7357
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7012
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3171
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1522
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
402
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.