Ok, im gonna create a include, for my database connection, just a couple of quries...

Firstly, creating a connection....is this ok for it:

<?php
//Database connection
	$user="";
	$host="";
	$password="";
	$database = "";
//If fail to connect, error
	$connection = mysql_connect($host,$user,$password)
			or die ("Couldn't connect to server");
	$db = mysql_select_db($database,$connection);
?>

And finally, you have it as a include file in the php script u want it included to, now, do you either have the

mysql_close();

function in the include file, or at the end of the php file you have the database connection included in?

Hope that all make sense, and i know its a dunse question 😛

    well you can't have the mysql_close() in the include file at the top of the page or the connection to the db server will be dropped before you ever do any queries. What you would do it have an include at the top of each page which kicks off everything that needs to be started and then another include at the bottom of each page which closes down all the things that need to closed down.

    Alternatively you could look into a slightly more complex structure (this is how I tend to organize pages). Let's take you're index page, for the actual index.php in the root web directory, this is all you'd have.

    <?php
    $PAGE['name']='index';
    //as many config variables as you wish,
    //prehapse something to say this page can only be accessed by registered users.
    $PAGE['user']=true;
    //then include your controller file
    include('./page/controller.php');
    ?>
    

    Then your controller page can tie everything together.

    <?php
    //include your function, class,session handling, global settings files
    include('header/headers.php');
    if($PAGE['user']) {
      //do checks to see if it's a registered user
    }
    //include your page logic file (always keep logic and display seperate
    include('./page/'.$PAGE['name'].'_logic.php');
    //iclude the standard header file
    include('./top/header.php');
    //include your page display file
    include('./page/'.$PAGE['name'].'_display.php');
    //include your footer to close everything off
    include('./bottom/footer.php');
    ?>
    

    See?
    Even if that seems like overkill now it's a good idea to thing about structuring your filesystem early on.
    HTH
    Bubble

      Write a Reply...