Page 1 of 1

Variables as table name?

Posted: Fri Jul 02, 2004 9:11 pm
by tomfra
I want to be able to define a table name prefix so that it's easy to change in the php script. The table name "suffix" / main name should stay the same.

But what is the correct format in the MySQL select?

Let's say that the table name is "pre_tblname". I define $tb_prefix such as:

$tb_prefix = "pre";

Now the SELECT part of the MySQL query should look something like:

Code: Select all

$query = "SELECT * FROM `$tb_prefix_tblname`";
But I know this is not the right format. So how can this be done?

Thanks!

Tomas

Posted: Fri Jul 02, 2004 9:43 pm
by feyd

Code: Select all

$query = "SELECT * FROM `{$tb_prefix}_tblname`";

Posted: Sat Jul 03, 2004 5:00 am
by tomfra
Thanks, it works :) Now I created one more problem because of it though... When I specify the $tb_prefix variable at the beginning of the script, it is inaccessible from within a while loop which is in a function.

Is it possible to read value of variable from within loop which is placed into a function, if that variable have been specified before / above the function? You know, I am still very much in a newbie mode ;)

Thanks again!

Tomas

Posted: Sat Jul 03, 2004 5:33 am
by Weirdan
you need to define that variable as global:

Code: Select all

$foo = 'bar';
function foobar() {
   global $foo; // <=======
   echo $foo;
}

Posted: Sat Jul 03, 2004 5:33 am
by feyd

Code: Select all

$somevar = "jelly";

function somefunc()
{
echo $somevar;
global $somevar;
echo $somevar;
//....
}