View Full Version : Call db table from any PHP file

02-19-2010, 11:06 AM

I want to be able to call a database table that will be setup in another file called init.php, and be able to call it from any PHP file that has init.php included.

Table is called abc_offline and has columns a,b,c in it.

What I am wanting is to be able to call it from any PHP file by:
$offline['a'] for example

How do I do this, I been told I need an array in init.php but I don't know how to do this.


02-19-2010, 11:09 AM
just connect to the DB (given that init.php provides the necessary settings (like password, etc.)) using the PHP functions responsible for connecting to your DB type. but unless we know more about init.php, that is all I can currently say.

02-19-2010, 11:12 AM

The DB connect is already setup.
At the moment all that is in init.php is the connect for the DB and some calls to the database for other tables

So I am looking for the code to calling the database and being able to call it for any file as $offline['a']


02-19-2010, 11:16 AM
what should $offline['a'] contain, a single value from the `a` field, or all its values? you just need to write some code that fetches that into this variable.

02-19-2010, 11:23 AM
Column a in abc_offline would have the value as offline or online
So what I want to do is, in any file be able to have

This code could be in any file
if ( $offline['a'] == 'online' ) { header ('location: index.php'); }
if ( $offline['a'] == 'offline' ) { header ('location: offline.php'); }

I need the code to go in the init.php as I haven't got a clue what to put, to be able to able to call the table and assign $offline to any file.

02-19-2010, 12:17 PM
Something like

$rstOffline = mysql_query('SELECT a FROM abc_offline');
$offline = mysql_fetch_assoc($rstOffline);

should be all you need if the database is already connected and selected. Assuming init.php is not inside a function this variable when then exist in a global scope.

When refering to it elsewhere you can either refer to it directly as $offline['a'] or if you are inside a function then either refer to it as $GLOBALS['offline']['a'] or at the top of the function put

global $offline;

Note that global variables should generally be used very sparingly, but this is the simplest answer to your direct question. A better solution may be to put the value into session or make it part of a "user" object that gets passed around, hard to say without knowing more about your code.



02-19-2010, 01:06 PM
Thanks, will give it a try later.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum