I do agree with Peter that it's not a good idea to use the numeric key to refer to fields returned, even though I mentioned it. Using a meaningful name is much preferable, for the reasons he gave and because it makes the code so much easier to read.
Ellen V. Coen Database Programmer Brooklyn Museum [EMAIL PROTECTED] 718.638.5000 x578 -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Sawczynec Sent: Wednesday, February 14, 2007 9:29 AM To: 'MySQL SIG' Subject: RE: [mysql] Syntax / PHP Right, do not use the field numerical index technique for retrieving data. If you or the next developer add a new column to a table you will likely mess up the numeric index count. I have come to use aliasing table names and column names even when it isn't 'necessary'. I find that later if I need to add another table join or add six new columns to two different tables. My queries are extremely flexible and take to these SQL changes easily. Lastly, by aliasing database table column names to some really friendly term to use in your PHP code (such as MySQL column name 'seq_id' to alias 'sequential_product_id'), when you come back 9 months later you still know exactly what you're doing. Peter -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rob Marscher Sent: Wednesday, February 14, 2007 9:20 AM To: MySQL SIG Subject: Re: [mysql] Syntax / PHP [EMAIL PROTECTED] wrote: > The tables I have are the following with the field 'Name' appearing in > each table: > > `section`.`Name` > `district`.`Name` > `building`.`Name` > > Then I have: > > $Section = $row[Name]; > $Building = $row[Name]; > > See my problem? "Name" is the same field name in two different tables. > I tried to do something like: You need to give an alias for each of those name columns in order to access it that way: Select section.Name as section_name, district.Name as district_name, etc... Then you can do: $Section = $row['section_name']; Alternatively, you can access them by the numerical index in $row, but that's not very reliable because if your query changes at all, you'll have to update your variable assignments. This is what I'm talking about: Select section.Name, district.Name, etc... $Section = $row[0]; $District = $row[1]; I'd recommend the first way. -Rob _______________________________________________ New York PHP Community MySQL SIG http://lists.nyphp.org/mailman/listinfo/mysql NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php _______________________________________________ New York PHP Community MySQL SIG http://lists.nyphp.org/mailman/listinfo/mysql NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php _______________________________________________ New York PHP Community MySQL SIG http://lists.nyphp.org/mailman/listinfo/mysql NYPHPCon 2006 Presentations Online http://www.nyphpcon.com Show Your Participation in New York PHP http://www.nyphp.org/show_participation.php
