Forum Moderators: coopster

Message Too Old, No Replies

using a variable to name a table

seeking advice

         

jackson

4:14 pm on Oct 16, 2003 (gmt 0)

10+ Year Member



hi,

I'm hacking through some PHP code at the moment. The queries there have been set up in this fashion:

$query = "INSERT into ".$EVENTS_TB." VALUES ...

There's nothing more to it other than declaring it as $EVENTS_TB = events and is used this way across the application.

Using a variable in this fashion is something I've rarely seen used elsewhere. Does something like this make sense? What are the implications involved, if any?

My instinctive feeling is use the table as it's named in database (mysql) and to get on with it from there.

Look forward to some opinions - TIA.

jatar_k

4:20 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use vars to access tables when I have one script handling multiple databases that are in the same application. In many of the dynamic sites I've built there may be various tables for different parts of the application that I can only access by prepending a var to a certain name to know what table to query.

So it's fairly normal.

Does it work? If so what in particular are you
worried about?

coopster

4:21 pm on Oct 16, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If the table name ever changed, it only has to be maintained in one place.

Or, say maybe somewhere down the road, the database programmers decide to use one of your table names as a keyword...OK, that could be far-fetched, but you get the drift.

jackson

4:52 pm on Oct 16, 2003 (gmt 0)

10+ Year Member



Thanks for responding so quickly. Your replies are really appreciated.

coopster, you hit it on the nail - as in making a single change should the table name ever change.

jatar_k - yes it works at present. As ever, the concern is security. But then, this is where my php newbie green horns show up. Not being let's say, visually familiar, with this sort of set up - it struck me as being strange. This app only uses 3 tables although it may become part of another CMS-type app later on. But, this wouldn't have any real bearing on the app itself.