Hi! Im making a database class in PHP. And i thought i might post it here, and ask for your opinions and how i can do this better? :)
<?php
class Database {
var $db_host = 'localhost'; // Server
var $db_user = 'root'; // Username
var $db_pass = ''; // Password
var $db_name = 'darqnet'; // Database
var $db_char = 'utf-8'; // Charset
var $db_link; // Connection is stored here
var $db_rslt; // Query result
var $db_numr; // Numrows of query
function __construct() {
$this -> db_link = mysql_connect(
$this -> db_host,
$this -> db_user,
$this -> db_pass
) or die('Kunne ikke koble til MySQL');
mysql_select_db(
$this -> db_name,
$this -> db_link
) or die('Kunne ikke åpne database ' . mysql_error());
mysql_set_charset(
$this -> db_char,
$this -> db_link
);
}
function query($sql) {
$statement = explode(' ', $sql);
switch ($statement[0]) {
case ($statement[0] == 'SELECT') :
$this -> db_rslt = mysql_query($sql, $this -> db_link);
$this -> db_numr = mysql_num_rows($this -> db_rslt);
break;
case ($statement[0] == 'UPDATE' || $statement[0] == 'INSERT') :
$this -> db_rslt = mysql_query($sql, $this -> db_link);
break;
}
}
function num_rows() {
return $this -> db_numr;
}
function rows() {
$rows = array();
for ($x = 0; $x < $this -> db_numr; $x++) {
$rows[] = mysql_fetch_assoc($this -> db_rslt);
}
return $rows;
}
}
?>