Hey there,

I was just looking at your code, and I also look at other's code, and I 
think people are making life difficult for themselves. Sure a lot of 
people on this list are beginners, but thats more te reason to simplify 
code...

One thing you should always do, is write functions... I have written a 
few functions to connect to a database, execute queries and return 
results, and insert and update queries. Using functions like these make 
it a lot easier to write working code, and because it's less typing, its 
a lesser chance of an error occurring...

for example... here is a portion of my lib.php which I include() on each 
page I create (some comments are removed to save space)

putting the below code into an external file and including it on every 
page makes everything a lot cleaner, and if you need to connect to a 
database, just call

dbConnect();

in your php script... you must make sure that in the external file, the 
constants are set with your server details... or you can do it in the 
actual page with the function, like so:

dbConnect("dbname","localhost","username","password");

and if you need the connection ID, it is in the variable $DB_CONN.
If you want some more functions like this (i have a few more called 
getData(), dbInsert(), dbUpdate() which all make it a LOT easier to 
transport data to and from a mysql database), email me and I will send 
them to you directly. If you don't run mySQL, then you can just modify 
the functions to suit your DB, or you can add and delete things inside 
the functions for your own setup.

Adam

/******** DEFINE CONSTANTS ********/

// Connecting to a database

define( "DB_SERVER"             ,       "localhost" ); // used for dbConnect()
define( "DB_USERNAME"   ,       "username" ); // used for dbConnect()
define( "DB_PASSWORD"   ,       "password" ); // used for dbConnect()
define( "DB_DATABASE"   ,       "dbname" ); // used for dbConnect()


/******** DATABASE FUNCTIONS ********/

function dbConnect($dbDatabase=DB_DATABASE, $dbServer=DB_SERVER, 
$dbUsername=DB_USERNAME, $dbPassword=DB_PASSWORD){
        
        global $DB_CONN;

        $DB_CONN = @mysql_connect($dbServer, $dbUsername, $dbPassword) or 
die("Couldn't connect to database server.");
        $db = @mysql_select_db($dbDatabase) or die("Couldn't select 
database.");
        
        return $DB_CONN;
}


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to