<?php
class database {
public function __construct() {
$this->dbhost = 'localhost';
$this->dbuser = 'xxxx';
$this->dbpass = 'xxxx';
$this->dbconn = null;
}
/**
* Connectie maken met de MySQL database
*/
public function connect($dbname) {
if(!$this->dbconn) {
if (!$this->dbconn = mysql_connect($this->dbhost, $this->dbuser, $this->dbpass)) {
throw new Exception('<p>Could not connect to the MySQL database!</p>');
} else {
if (!mysql_select_db($dbname)) {
throw new Exception('<p>Could not find specified database!</p>');
}
}
}
}
/**
* Query uitvoeren, return $result
*/
public function query($sql) {
if(!$result = mysql_query($sql, $this->dbconn)) {
throw new Exception('<p>Could not execute query! MySQL error: ' . mysql_error() . '</p> ');
} else {
return $result;
}
}
/**
* Fetch assoc(array) doormiddel van de resultaten van de query
*/
public function FetchAssoc($sql) {
if(!$result = mysql_fetch_assoc($sql)) {
throw new Exception('<p>Could not execute query!</p>');
} else {
return $result;
}
}
/**
* Fetch row array doormiddel van de resultaten van de query
*/
public function FetchRow($sql) {
if(!$result = mysql_fetch_row($sql)) {
throw new Exception('<p>Could not fetch row!</p>');
} else {
return $result;
}
}
/**
* Tel het aantal rijen in de resultaten van de query
*/
public function NumRows($sql) {
if(!$result = mysql_num_rows($sql)) {
throw new Exception('<p>Could not count the number of rows!</p>');
} else {
return $result;
}
}
}
?>
1.177 views
Omdat ik niet zo heel lang met php werk en ik vorige week een MySQL database klasse deels heb gemaakt wil ik graag weten of het goed is, of er nog verbeter punten zijn of dat het gewoon slecht is en ik beter opnieuw kan beginnen.