Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > PHP, Objects, SQL queries & AttributeNames

PHP, Objects, SQL queries & AttributeNames
Thread Tools
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Nov 27, 2002, 05:42 AM
 
I've taken up PHP/SQL recently - I was wondering if someone could answer a question:

Imagine the following tables:
Code:
Foo ID FooName pictureID Description 1 hello 1 Hello World Picture ID Name PictureFileName Description 1 helloPic hello.jpg A picture of Hello
I've been using something like the following (simplified) code:

Code:
… $query="SELECT * FROM Foo, Picture WHERE foo.pictureID=Picture.id" $result = mysql_query ( $query, $connection); while ($resultObject=mysql_fetch_object($result)) { echo "Foo name: $resultObject->FooName"; echo "some other value: $resultObject->name"; } …

the code works - but if I were to try and extract $resultObject->Description then it wouldn't work - because there are Description attributes in both tables.

I am currently solving this by using a NameSpace for table attributes like so:

attributeTitle = TableName+AttributeTitle

so Description (in the Foo table) becomes FooDescription and Description in the Picture table becomes PictureDescription. - Similarly id becomes - FooID or PictureID.

This works, but attribute names quickly become very long.

Basically my question is this:

Is this the right way of going about it?
You know it makes sense. ☼ ☼ ☼ Growl.
     
Forum Regular
Join Date: Mar 2001
Status: Offline
Reply With Quote
Nov 29, 2002, 11:34 PM
 
Well, I always use standard column names in my tables and alias them in my queries, like so:

Code:
$res = mysql_query("select foo.*, foo.description as foodesc, picture.* from foo, picture where foo.pictureid=picture.id", $link); while($row = @mysql_fetch_array($res)) { print("foo description: ".$row['foodesc']); print("picture description: ".$row['description']); }
It makes your SQL queries longer, but I find it easier to manage my database if the tables use a standard naming convention. And you don't need to go back and rename your tables or columns if you have a namespace conflict later.
Is this rock and roll, or
a form of state control?
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Nov 30, 2002, 10:57 AM
 
Thanks so much - I had no Idea you could alias column names.
You know it makes sense. ☼ ☼ ☼ Growl.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 06:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2