Hey Everyone,
I'm in the process of creating a nice mySQL database class and am interested in your opinions on it so far. Please let me know what you think and what else you feel should be added/fixed. This was coded in PHP5.
mysql.class.php:
PHP Code:
<?php
/* mySQL Database Class
Mike Froseth
Testing Purposes Only (for now!)
*/
class mysql {
private $linkid; // mySQL link identifier
private $host; // mySQL server host
private $user; // mySQL user
private $pswd; // mySQL password
private $db; // mySQL database
private $result; // mySQL result
private $querycount; // Total queries executed
/* Class constructor. Initializes the $host, $user, $pswd
and $db fields. */
function __constuct($host, $user, $pswd, $db) {
$this->host = $host;
$this->user = $user;
$this->pswd = $pswd;
$this->db = $db;
}
/* Connects to the mySQL database server */
function connect() {
try {
$this->linkid = @mysql_connect($this->host,$this->user,$this->pswd);
if(! $this->linkid)
throw new Exception("Could not connect to the mySQL server.");
}
catch (Exception $e) {
die($e->getMessage());
}
}
/* Selects the mySQL database. */
function select() {
try {
if (! @mysql_select_db($this->db, $this->linkid))
throw new Exception("Could not connect to the mySQL database.");
}
catch (Exception $e) {
die($e->getMessage());
}
}
/* Execute database query */
function query($query) {
try {
$this->result = @mysql_query($query,$this->linkid);
if (! $this->result)
throw new Exception("The database query failed.");
}
catch (Exception $e) {
echo($e->getMessage());
}
$this->querycount++;
return $this->result;
}
/* Determine total rows affected by query. */
function affectedRows() {
$count = @mysql_num_rows($this->linkid);
return $count;
}
/* Determine total rows returned by query. */
function numRows() {
$count = @mysql_num_rows($this->result);
return $count;
}
/* Return query result row as an object. */
function fetchObject() {
$row = @mysql_fetch_object($this->result);
return $row;
}
/* Return query result as an indexed array. */
function fetchRow() {
$row = @mysql_fetch_row($this->result);
return $row;
}
/* Return query result row as an associative array. */
function fetchArray() {
$row = @mysql_fetch_array($this->result);
return $row;
}
/* Return total number queries executed during lifetime of this object.
Not required, but interesting nonetheless. */
function numQueries() {
return $this->querycount;
}
}
?>
Thats it so far. Please let me know what you think and any suggestions you might have.
Regards,