Page 1 of 1
Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 1:34 pm
by dv_evan
Hello All,
I am very new to php/mysql, in fact I am now learning by using online tutorials and code snippets.
I came came across a code that have me a very confused and would like if someone can explain:
Select * From $DBPREFIX;
I know about the select statment and the fields and table name, but I am failing to interprete the ' $DBPREFIX ' .
I tried to trace where this variable was set in the codes, but all I have is it was declare as a global variable. Can someone please explain?
Thanks a million for any help.
Regards
Dave.
Re: Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 1:49 pm
by flying_circus
If it was declared in a higher scope, then it is likely pulling that value for your SELECT statment.
Not really sure of your question, since you say you found where the variable is declared.
Can you post up the code in question? It's not uncommon to have a global config file so that the script may be easily implmented on just about any webserver.
Re: Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 2:09 pm
by dv_evan
Thanks Flying_Circus,
The situation is that I have myself a set of working codes comprised of many .php files.
Here is the code snippet where I saw the declaration;
<?php
$Server = "localhost:3306";
$Db = "User_Db";
$User = "admin";
$Password = "password";
$ClientName = "Sports Inc";
$DBPREFIX = "Dive";
$DBase = "MYSQL";
if ($DBase == "ODBC") {
$CURRENT_DATETIME = date("m/d/y g:i a");
} elseif ($DBase == "MYSQL") {
$CURRENT_DATETIME = date("Y-m-d H:i:s");
}
?>
Re: Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 2:25 pm
by dv_evan
I think I get the concept, but it is confusing.
$DBPREFIX = Dive;
$Query = "SELECT User_name FROM " . $DBPREFIX . "User";
$Query2 = "SELECT Lake FROM " . $DBPREFIX . "Location";
There are Several databases eg. DiveUser, DiveLocation, DiveArticle etc. As you can see, this coder use the $DBPREFIX to separate the table names as " . $DBPREFIX . "User" and " . $DBPREFIX . "Location" to use in the query.
Do anyone has any idea why this was done?
Thks
Dave
Re: Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 4:00 pm
by onion2k
The coder did it so that the database tables could be used in a shared database. If you only have access to one database (in a shared environment for instance) you can't call your users table "users" because there's probably already a table with that name used by a different application. So you have to give your tables unique names ... the easiest way to do that is to prefix them all with an application specific id, eg 'dive', then define that prefix in the constants and use it throughout the application. That's preferable to calling your tables things like "diveUser" in the actual code because you might decide to change it later.
Re: Newbie explanation needed - $DBPREFIX
Posted: Mon Apr 21, 2008 7:29 pm
by dv_evan
Thanks a lot , Onion
That is what I had thought, I appreciate the explanation.
Dave