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