PHP has the abilty to include other PHP files into the current script that is being processed by the server. Let us just take a simple example. This will be a file that will connect yo your database and a specified table. It will include all the necessary parameters to actually do just that. This is a program on its own right and as such will perform a specific funtion (connect to the MySQL Server and select a database to work with).
So for testing purposes we will create this file to be included when the script we are using needs this to happen. Look at it as its own program (which it is) that helps save typing a new program. Usually you would like this program or include file to ahave a name that is not easily recognized as a database connection, so call it something like error.php or happy.php and to really throw off hackers you could also include a file that doesn't even do anything and call it db.php (this is a file with nothing at all in it). Security matters aside then there are other ways to protect this important file which will be explaines later in the tutorial. So lets build the connection file for inclusion where and when needed. NOTE anything in the code below behind // is a comment and can be copyed and pasted into a working script
CODE
<?php//always use this in your script <? is deprecated and unreliable
$host="localhost";//This will work with most MySQL servers but your server might be different
$user="root";//This is a default for a new MySQL install use you own username if assigned one
$password="";//This is a default, if you have a password for your MySQL USE IT
$db="yourDatabase";//The name of the datebase you want to connect to goes here between the double quotes
Ok now name the above script whatever you want and use it as an include in other scripts where and when you need it like below.$host="localhost";//This will work with most MySQL servers but your server might be different
$user="root";//This is a default for a new MySQL install use you own username if assigned one
$password="";//This is a default, if you have a password for your MySQL USE IT
$db="yourDatabase";//The name of the datebase you want to connect to goes here between the double quotes
CODE
<?php
include("yourIncludeScript.php");
$connect =($host,$user,$password)
or die("Could not connect with server, check settings, MySQL said: ".mysql_error());
//If anything is wrong the file will stop and show an error Could not connect with server, check settings, MySQL said: and the actual MySQL error.
$query ="SELECT * FROM $db";//This is from the included file as is the variable from above
$result =mysql_query($query)
or die("Execution of query failed".mysql_error());
...//more code
So how does this look to PHP on the server which is what this little tutorial is about?include("yourIncludeScript.php");
$connect =($host,$user,$password)
or die("Could not connect with server, check settings, MySQL said: ".mysql_error());
//If anything is wrong the file will stop and show an error Could not connect with server, check settings, MySQL said: and the actual MySQL error.
$query ="SELECT * FROM $db";//This is from the included file as is the variable from above
$result =mysql_query($query)
or die("Execution of query failed".mysql_error());
...//more code
CODE
<?php
//here is the include file as specified see above code
$host="localhost";//This will work with most MySQL servers but your server might be different
$user="root";//This is a default for a new MySQL install use you own username if assigned one
$password="";//This is a default, if you have a password for your MySQL USE IT
$db="yourDatabase";//The name of the datebase you want to connect to goes here between the double quotes
//end of the includeed file
$connect =($host,$user,$password)
or die("Could not connect with server, check settings, MySQL said: ".mysql_error());
//If anything is wrong the file will stop and show an error Could not connect with server, check settings, MySQL said: and the actual MySQL error.
$query ="SELECT * FROM $db";//This is from the included file as is the variable from above
$result =mysql_query($query)
or die("Execution of query failed".mysql_error());
...//more code[
As you can see the included file is parsed by PHP as it was originally written, mistakes and comments included. If you do include a file that does not work you need to correct it before using it as an include. Many problems with using PHP are includes with flaws in them or used in the wrong place. Had the include been at the bottom of the above script the values needed to connect to the database had not been defined, so keep in mind where and when you need to include a file with a specific purpose when written your script in PHP. //here is the include file as specified see above code
$host="localhost";//This will work with most MySQL servers but your server might be different
$user="root";//This is a default for a new MySQL install use you own username if assigned one
$password="";//This is a default, if you have a password for your MySQL USE IT
$db="yourDatabase";//The name of the datebase you want to connect to goes here between the double quotes
//end of the includeed file
$connect =($host,$user,$password)
or die("Could not connect with server, check settings, MySQL said: ".mysql_error());
//If anything is wrong the file will stop and show an error Could not connect with server, check settings, MySQL said: and the actual MySQL error.
$query ="SELECT * FROM $db";//This is from the included file as is the variable from above
$result =mysql_query($query)
or die("Execution of query failed".mysql_error());
...//more code[
Questions or for more about using an include(s) then PM me Houdini for more. Been working on other things lately, so now I am back for most questions about PHP and MySQL useage.


