B
Bgag
Abend!
Ich habe eine kleine Datenbank-Klasse geschrieben und hätte gern ein kleines Feedback. Ist das so gut oder ok? Was kann man noch ändern? Was kann man noch verbessern oder hinzufügen?
Wäre euch für eure Hilfe dankbar!
MfG, Andy
Ich habe eine kleine Datenbank-Klasse geschrieben und hätte gern ein kleines Feedback. Ist das so gut oder ok? Was kann man noch ändern? Was kann man noch verbessern oder hinzufügen?
PHP:
<?php
class DbConnector {
/* DATABASE VARIABLES */
var $host = "localhost";
var $user = "user";
var $password = "pwd";
var $dbname = "dbname";
/* VARIABLES */
var $result;
var $theQuery;
var $link;
var $result_number;
var $db_status = "disconnected";
var $errormessage = "";
var $errornumber = "";
/* CATCH DATABASE VARS */
function DbConnector($host,$user,$pw,$dbname) {
$this->host = $host;
$this->user = $user;
$this->password = $pw;
$this->dbname = $dbname;
$this->db_open();
}
/* CONNECT TO DATABASE */
function db_open() {
if($this->db_status != "connected") {
if(!($this->link = mysql_connect($this->host, $this->user, $this->password))) {
$this->db_status = "disconnected";
$this->errormessage .= mysql_error() ."\n";
$this->errornumber .= mysql_errno() ."\n";
$this->password = "******";
return 0;
}
else {
$this->db_status = "connected";
$this->db_select_db($this->dbname);
$this->password = "******";
return 1;
}
}
else {
echo "Critical ERROR: You opened DB 2 times";
$this->password = "******";
return 0;
}
}
/* SELCT DATABASE */
function db_select_db($dbname) {
if($this->db_status == "connected") {
if(!mysql_select_db($dbname)) {
$this->errormessage .= mysql_error() ."\n";
$this->errornumber .= mysql_errno() ."\n";
return 0;
}
else return 1;
}
else return 0;
}
/* CHECK CONNECTION */
function is_connected() {
return($this->db_status == "connected");
}
/* EXECUTE DATABASE QUERY */
function db_query($query) {
$this->theQuery=$query;
if($this->db_status == "connected") {
if(!$this->result = mysql_query($query)) {
$this->errormessage .= mysql_error() ."\n";
$this->errornumber .= mysql_errno() ."\n";
return 0;
}
else {
$query = trim($query);
if(substr(strtoupper($query), 0, 4) == "SELE") $this->result_number = mysql_num_rows($this->result);
return 1;
}
}
else return 0;
}
/* RETURNS LATEST DATABASE QUERY */
function getQuery() {
return $this->theQuery;
}
/* RETURN ROW COUNT */
function getNumRows($result){
return mysql_num_rows($result);
}
/* GET ARRAY OF QUERY RESULTS */
function db_save_results($typ = 0) {
$array = array();
while ($result = $this->db_get_next_result($typ)) $array[] = $result;
return $array;
}
/* GET NEXT RESULT OF LAST QUERY */
function db_get_next_result($typ = 0) {
if(!$this->result) {
$this->errormessage .= "Result is empty.\n";
$this->errornumber .= 99 ."\n";
return 0;
}
else {
if($typ == 0) return mysql_fetch_array($this->result);
if($typ == 1) return mysql_fetch_row ($this->result);
if($typ == 2) return mysql_fetch_assoc($this->result);
}
}
/* GET LAST ERRORS */
function db_get_last_error() {
if($this->errornumber != "" || $this->errormessage != "")
return ($this->errornumber . " : " . $this->errormessage);
else return "";
}
/* CLOSE CONNECTION */
function close() {
if($this->db_status == "connected") {
if(!mysql_close($this->link)) {
$this->errornumber .= mysql_errno() ."\n";
$this->errormessage .= mysql_error() ."\n";
$this->password = "******";
return 0;
}
else {
$this->db_status = "disconnected";
$this->password = "******";
return 1;
}
}
else {
$this->password = "******";
return 0;
}
}
}
?>
Wäre euch für eure Hilfe dankbar!
MfG, Andy
Zuletzt bearbeitet von einem Moderator: