Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 15th, 2007, 09:28 AM
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
  #2 (permalink)  
Old May 13th, 2008, 09:24 AM
Friend of Wrox
Points: 1,515, Level: 15
Points: 1,515, Level: 15 Points: 1,515, Level: 15 Points: 1,515, Level: 15
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2003
Location: Oxford, , United Kingdom.
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You include the file with the connect function in it, but you never call the function.

Try:
Code:
include('testcon.inc.php'); // include file
$connection = connect(); // call function
if ($connection) {
  echo ("Connection created ...");
} else {
  echo ("Error connecting to database!");
}
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code Efficiency rsm42 ASP.NET 1.0 and 1.1 Basics 12 August 8th, 2007 11:36 AM
code efficiency sxstat90 SQL Language 1 March 8th, 2005 04:42 PM
efficiency qn - writing and saving xml file ak Classic ASP XML 1 March 2nd, 2004 05:26 PM
Function Efficiency Ben Access VBA 3 November 20th, 2003 04:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.