Thread: Efficiency
View Single Post
 
Old November 15th, 2007, 09:28 AM
Kev_Akas Kev_Akas is offline
Registered User
 
Join Date: Apr 2006
Location: Newcastle upon Tyne, , United Kingdom.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Kev_Akas Send a message via Yahoo to Kev_Akas
Default Efficiency

Hi people, I'm writing something and I want to make my code as efficient as possible... this is the code I've done so far to connect to a database and create a table.

This is the included file;
Code:
<?php
    function connect()
    {
        $db = "appraisals"; //This is the database

        $mysqlhost = "localhost"; //The host which the database is installed on
        $mysqluser = "appraisals"; //Username for the database
        $mysqlpw = "appraisals"; //Password for the database


        $connection = @mysql_pconnect($mysqlhost, $mysqluser, $mysqlpw) or die //Attempt to connect, if no - report error
        ("Unable to connect to the database.");

        if($connection)
        {
            mysql_select_db($db);
            return $connection;
        }
        else
        {
            exit();
        }
    }
?>
and the code to create the table;
Code:
<?php
//Connect to the database to create the sample table:
$connection = include('testcon.inc.php');
if ($connection) {
  echo ("Connection created ...");
} else {
  echo ("Error connecting to database!");
}
$create_table_string = "CREATE TABLE sample (".
                        "`fullname` VARCHAR(50), ".
                        "`jobtitle` VARCHAR(50), ".
                        "`salesoffice` VARCHAR(50), ". 
                        "`email` VARCHAR(50), ".
                        "`password` VARCHAR(50), ".
                        "`access` VARCHAR(50), ".
                        "PRIMARY KEY (email))";                    

$connection = mysql_connect (localhost, appraisals, appraisals);    
mysql_db_query ( "appraisals", $create_table_string )||
 die ("Can't create the table!");
echo ("Table created successfully.");
?>
Now as you can see before the following;
Code:
mysql_db_query ( "appraisals", $create_table_string )||
I have to reconnect to the database, as it appears to close and I get the access denied error, unless I re-specify the connection.

Can anyone help me find out what I'm doing wrong? I cannot re-use;
Code:
$connection = include('testcon.inc.php');
Kev Atkinson
http://www.kevatkinson.co.uk