View Full Version : Information on writing DB include_once for PHP

11-04-2012, 06:35 AM
Hi All,

I am just wondering if their is anyway I can write all the db variables at one file and call them in the respective scrip.

Actually i have seen couple of scripts designed in the same way but I am not understanding how they were called at required place ??

FYI...attching the script.

Please help me by sharing any tutorial if you came across in the same way... because I have a DB which as multiple tables related to individual script so, if i want to change the user ID or any other details I need to change it in all the script spite if i can store all this dable name at one place it would be easy for me to manage the administration so, I really want to understand this ....

appreciated any help :).

11-04-2012, 10:30 AM
Are you familiar with using classes in PHP? It's a great way to group related stuff like variables and that is the method the files you attached are using. If you're interested in improving the structure of your scripts that would be a great place to start. You could try the official documentation (http://www.php.net/manual/en/language.oop5.basic.php), or a simple Google search might have some easier to understand tutorials.

However, there are definitely simpler methods. This should work:



$variable = "This is a variable.";

define( "CONSTANT" , "This is a constant." );


.. And then in some public file..

<?php include_once "file.php"; ?>

<title> Your own HTML, obviously. </title>

<p> <?php echo $variable; ?> </p> // Outputs "This is a variable."
<p> <?php echo CONSTANT; ?> </p> // Outputs "This is a constant."


If the value you're intending to use isn't being calculated, altered by the script, or otherwise quite literally variable in nature, then using constants is the best practice.