473,725 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Table.fieldname syntax doesn't work?

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 index.

Or am I doing something wrong (I hope so!)? I can't find any mention
of this problem in any docs, so it's totally possible that I'm goofing
something up without realising it. If it's a real problem, it's an
annoying one because it means we must (a) name all fields uniquely
even across tables, (b) avoid doing joins, or (c) get used to using
the numeric indexes to the fields instead of their names.

My test program:
$link = mysql_connect( $_SERVER['HTTP_HOST'] ) ;
if ( empty( $link ) ) die( 'Cannot connect to the server' ) ;
$attached = mysql_select_db ( 'dbase' ) ;
if ( ! $attached ) die ( 'Cannot connect to the database' ) ;

$q = 'select i.id, s.id from table_issues as i, table_scopes as s
where i.id=3 and i.scope=s.id' ;
$dset = mysql_query( $q, $link ) ;
if ( $dset )
{
$rec = mysql_fetch_arr ay( $dset ) ;
foreach ( $rec as $k => $v )
echo $k . ' is ' . $v . '<br>' ;
}

yields this result:
0 is 3
id is 129
1 is 129
Thanks in advance for any insights.
Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #1
6 2895
Margaret MacDonald wrote:
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'

(snip)
The correct syntax is

select col1 AS col_alias1,
col2 AS col_alias2,
...
from table1 table_alias1,
table2 table_alias2,
...

so your example would be
SELECT a.id, b.id FROM table1 a, table2 b

or, if you'd like to rename the columns
SELECT a.id as table1_id, b.id as table2_id from table1 a, table2 b
HTH
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #2
On Mon, 05 Apr 2004 22:30:57 GMT, Margaret MacDonald
<sc**********@a tt.not.invalid> wrote:
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 index.


This is how it works in every database interface I have used; table
names/aliases are discarded, leaving only the column name. If you have multiple
columns having the same name, you need to alias them.

SELECT a.id AS a_id, b.id AS b_id
FROM table1 AS a, table2 AS b

This is documented, see:
http://uk.php.net/manual/en/function...etch-array.php

"
If two or more columns of the result have the same field names, the last column
will take precedence. To access the other column(s) of the same name, you must
use the numeric index of the column or make an alias for the column. For
aliased columns, you cannot access the contents with the original column name
(by using 'field' in this example).

Example 1. Query with aliased duplicate field names

SELECT table1.field AS foo, table2.field AS bar FROM table1, table2
"

One school of thought for column naming (that I agree with) is that two
identically named columns in different tables generally implies that there's a
foreign key relationship between the two, and so you wouldn't have two columns
named 'id' in two tables unless you could join on them. If you're joining them
together, then it doesn't matter which of the two you get, as they'd be equal.

So you might have table widget and table thingy:

thingy
------
thingy_id (pk)

widget
------
widget_id (pk)
thingy_id (fk to thingy)

select widget_id, thingy_id
from widget
join thingy using (thingy_id)

There's thingy.thingy_i d and widget.thingy_i d, but no conflict as to which one
to use they're the same value.

--
Andy Hassall <an**@andyh.co. uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #3
Pedro Graca wrote:
Margaret MacDonald wrote:
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'

(snip)
The correct syntax is

(snip bunch of BS)
Oops ... sorry everyone
The "AS" may be used between the tablename and the alias

I just never used it and didn't bother to check the manual
http://www.mysql.com/doc/en/JOIN.html

I'll read your post again Margaret.
Sorry again
--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #4
Margaret MacDonald wrote:
(snip)
$q = 'select i.id, s.id from table_issues as i, table_scopes as s
where i.id=3 and i.scope=s.id' ;
$dset = mysql_query( $q, $link ) ;
if ( $dset )
{
$rec = mysql_fetch_arr ay( $dset ) ;
foreach ( $rec as $k => $v )
echo $k . ' is ' . $v . '<br>' ;
}

yields this result:
0 is 3
id is 129
1 is 129


Andy is right.
There is no way to get the value using the string index if the columns
have the same name.

$q = 'select i.id as i_id, s.id as s_id from ...';

would yield:
0 is 3
i_id is 3
1 is 129
s_id is 129

--
USENET would be a better place if everybody read: : mail address :
http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
http://www.expita.com/nomime.html : to 10K bytes :
Jul 17 '05 #5
Thanks, both.

I didn't interpret the passage Andy quoted as being applicable
because, to me, the fully-qualified identifiers t1.xx and t2.xx
*aren't* the same--they're completely different to one another (which
of course is the whole point of the qualification).

I still think it's a bug or at least lacuna in the library, though,
since the qualifiers are present in the dataset when it's returned by
mysql.

So it's back to using unique fieldnames across all tables. Bummers.

Margaret
--
(To mail me, please change .not.invalid to .net, first.
Apologies for the inconvenience.)
Jul 17 '05 #6
"Margaret MacDonald" <sc**********@a tt.not.invalid> wrote in message
news:5j******** *************** *********@4ax.c om...
Thanks, both.

I didn't interpret the passage Andy quoted as being applicable
because, to me, the fully-qualified identifiers t1.xx and t2.xx
*aren't* the same--they're completely different to one another (which
of course is the whole point of the qualification).

I still think it's a bug or at least lacuna in the library, though,
since the qualifiers are present in the dataset when it's returned by
mysql.

So it's back to using unique fieldnames across all tables. Bummers.


You don't need to use unique field names. You merely need to alias them
(making them unique in the result set) in the queries when you happen to
join two tables having fields with the same name.

select t1.name as t1_name, t2.name as t2_name from company t1, employee t2
where t1.compid = t2.compid

or so...

- Virgil
Jul 17 '05 #7

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

Similar topics

5
2379
by: Michael Hobbs | last post by:
Yeah, yeah, another X-Oriented paradigm, but please hear me out. I have recently been studying up on EJB's in order to extend my resume. I have looked at J2EE several times over the past years, but have never been impressed by it. Now that I've studied it in detail, I know why. A strongly typed language such as Java is poorly suited to model tables and relations. I got to thinking that a more dynamically typed language, such as...
8
9729
by: David Housman | last post by:
Hi, I'm trying to write a function check if a column exists on a table, and creates it if it doesn't. The line that the query analyzer is citing is noted. It seems unhappy taking variables in the ALTER TABLE command. I can think of a hack around this, but I'm hoping there's a better way? Muchas gracias in advance :) -DJ Code follows
16
2157
by: Feico | last post by:
Dear friends I am an experienced programmer, but I happen to have MS Access, which uses a language unknown to me. I want to perform an operation on all record on a table, like this For recordnumber=1 to NumberOfElements(Tabel) PerformCalculation(recordnumber.fieldname)
1
1356
by: Stephen D Cook | last post by:
I have a form with a query linked subform. Also on the form is a textbox. I want to pull up an AddItem subform if the data in the textbox does not exsist in the table, and a UpdateItem subform if the item does exsist. I believe I would put the code in the AfterUpdate part of the textbox function. With something like If(Me.txtItem = "") Then
2
2377
by: clickon | last post by:
I am confused about the way in which asp:table objects work. When a control is within an asp table it generally appears to be in the scope of the tables parent control. E.g. if i have a page that consists of an asp table called MyTable containing a textbox control call MyTextBox then i can access MyTextBox directly from the PageLoad event handler without having to use MyTable.FindControl(MyTextBox). MyTextBox can also be used for a...
0
1065
by: Kosmos | last post by:
Okay so the following is a question I posted earlier...to clarify what I'm trying to do here...I simply want the printed output to be put into a new field in a different table that's already created as recSet2...I'm assuming this is relatively simple and my syntax just is incorrect or perhaps the way I'm opening my connections allows for a read only connection? Any help would be much appreciated. Thanks.
1
10162
by: andy | last post by:
Hi All Am new to sql server to sobear with me, have checked around but cant find an answer to this. I want to change fieldname from nvarchar(50) to nvarchar(255) as part of a script Am running this command : ALTER TABLE MYTABLE MODIFY fieldname nvarchar(255) null
2
1723
by: sparks | last post by:
last month I started on changing the format in a LOT of tables. changing Long Integer and Singles to double. I got that working but I have one more question.. Sub AlterFieldType(TblName As String, FieldName As String, DataType As _
3
1562
by: Sheldon | last post by:
How can this be done and what is the syntax? Thanks, Sheldon Potolsky
0
8888
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
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9174
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
9111
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
8096
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
6702
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
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.