473,657 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mysql_fetch_arr ay and table aliases

I'm using mysql_fetch_arr ay 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_arr ay($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 3783
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_arr ay($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
2888
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 fields as 'a.id' and 'b.id' the reference fails because the table aliases have been discarded from the dataset as managed by php. 'a.id' has become merely 'id', and 'b.id' no longer has a fieldname -- the only way to refer to it is by the numeric...
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. I run the following query with aliased columns with CLI: select table1.c1 as 'pet category', table1.c2 as 'item', table1.c3 as 'quantity', table2.c2 as 'attendant' from table1 left outer join table2 on (table1.c1=table2.c1);...
4
12291
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 @patient TABLE
16
3485
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 in a hidden field) I wish the user to be able to edit the data in the table, so I have extracted the records into hiddenfield, textareas, dropdown list and checkbox so that they can make changes. I named these elements as arrays and wish to run an...
3
5151
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 provide a front end to an Oracle database Also, does Access have the ability to create table Aliases for whenever you create a query in design view and then view it in SQL view (or does it have the ability to assign table aliases at all)
6
1418
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
4738
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 vendor has been added to the table yet. The problem is, if they haven't been added, I can't seem to get the script to realize that. here is what I am trying to do. $testvend = SELECT language FROM vendor_details WHERE id = $vendorid ...
1
1712
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
3282
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 manual that corresponds to your MySQL server version for the right syntax to use near 'date']; ?>','3:00am')' at line 1 The one that i bold and underline is Line 24 <?php session_start();
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2732
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 we have to send another system

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.