Newbie explanation needed - $DBPREFIX

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Newbie explanation needed - $DBPREFIX

Post 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.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Newbie explanation needed - $DBPREFIX

Post 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.
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Newbie explanation needed - $DBPREFIX

Post 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");

}


?>
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Newbie explanation needed - $DBPREFIX

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Newbie explanation needed - $DBPREFIX

Post 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.
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Newbie explanation needed - $DBPREFIX

Post by dv_evan »

Thanks a lot , Onion
That is what I had thought, I appreciate the explanation.

Dave
Post Reply