Save the following code as something like db_connect.php
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php abstract class DBName { public static $host = "localhost"; public static $user = "USERNAME"; public static $pw = "PASSWORD"; public static $con; public static function connect() { self::$con = mysql_connect(self::$host, self::$user, self::$pw); if (!self::$con) { die('Could not connect: ' . mysql_error()); } $dbname = "DATABASE NAME"; mysql_select_db($dbname, self::$con); } public static function disconnect() { mysql_close(self::$con); } } ?> |
Replace the host, username, and password with your own, and then replace DATABASE NAME with the database you will be working with.
Include it in any file working with database connections: include("db_connect.php");
After this file is included you may do all of your database work between these two calls:
|
1 |
DBName::connect(); |
Database manipulation here…
|
1 |
DBName::disconnect(); |