Snippet Name: Simple DB connection class
Description: A simple DB connection class with all often used mySQL functions.
Comment: (none)
Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: March 01st, 2009
|
<?PHP
CLASS DB_Class {
VAR $db;
///////////////////////////
FUNCTION DB_Class($dbname, $username, $password) {
$this->db = mysql_connect ('localhost', $username, $password)
or DIE ("Unable to connect to Database Server");
mysql_select_db ($dbname, $this->db) or DIE ("Could not select database");
}
FUNCTION query($sql) {
$result = mysql_query ($sql, $this->db) or DIE ("Invalid query: " . mysql_error());
RETURN $result;
}
///////////////////////////
FUNCTION fetch($sql) {
$data = ARRAY();
$result = $this->query($sql);
WHILE($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
RETURN $data;
}
///////////////////////////
FUNCTION getone($sql) {
$result = $this->query($sql);
IF(mysql_num_rows($result) == 0)
$value = FALSE;
ELSE
$value = mysql_result($result, 0);
RETURN $value;
}
///////////////////////////
}
?>
// usage example....
<?PHP
$dbconnect = NEW DB_Class('table', 'user', 'password');
$query = "SELECT user_id FROM user_table WHERE $match = $search ORDER BY user_id DESC";
$result = $dbconnect->fetch($query);
?> |