tblName = "none"; $this->fldName = "none"; $this->fldValue = "none"; $this->cndValue = "none"; $this->ordBy = "none"; $this->qryType = "none"; $this->dbHost = $dbHost; $this->dbName = $dbName; $this->dbUser = $dbUser; $this->dbPasswd = $dbPasswd; if (!isset($this->dbDieOnFail)) { $this->dbDieOnFail = true; } if (!isset($this->dbDebug)) { $this->dbDebug = false; } $this->dbLink = $this->db_connect($this->dbHost, $this->dbName, $this->dbUser, $this->dbPasswd); } function db_connect($dbHost, $dbName, $dbUser, $dbPasswd) { /* connect to the database $dbName on $dbHost with the user/password pair * $dbUser and $dbPasswd. */ if (! $this->dbLink = mysql_pconnect($dbHost, $dbUser, $dbPasswd)) { if ($this->dbDebug) { echo "
MySQL Error: ", mysql_error(); } else { echo "
This script cannot continue, terminating."; die(); } } if (!mysql_select_db($this->dbName, $this->dbLink)) { if ($this->dbDebug) { echo "
MySQL Error: ", mysql_error(); } else { echo "
This script cannot continue, terminating."; die(); } } return $this->dbLink; } function db_disconnect() { /* disconnect from the database, we normally don't have to call this function * because PHP will handle it */ mysql_close(); } function db_query($query, $debug=false, $die_on_debug=true, $silent=false) { /* run the query $query against the current database. if $debug is true, then * we will just display the query on screen. if $die_on_debug is true, and * $debug is true, then we will stop the script after printing the debug message, * otherwise we will run the query. if $silent is true then we will surpress * all error messages, otherwise we will print out that a database error has * occurred */ if ($debug) { echo "
" . htmlspecialchars($query) . ""; if ($die_on_debug) die; } $qid = mysql_query($query); if (! $qid && ! $silent) { if ($this->dbDebug) { echo "
" . htmlspecialchars($query) . ""; echo "
MySQL Error: ", mysql_error(); } else { echo "
This script cannot continue, terminating."; die(); } } return $qid; } function db_fetch_array($qid) { /* grab the next row from the query result identifier $qid, and return it * as an associative array. if there are no more results, return FALSE */ return mysql_fetch_array($qid); } function db_fetch_row($qid) { /* grab the next row from the query result identifier $qid, and return it * as an array. if there are no more results, return FALSE */ return mysql_fetch_row($qid); } function db_fetch_object($qid) { /* grab the next row from the query result identifier $qid, and return it * as an object. if there are no more results, return FALSE */ return mysql_fetch_object($qid); } function db_num_rows($qid) { /* return the number of records (rows) returned from the SELECT query with * the query result identifier $qid. */ return mysql_num_rows($qid); } function db_affected_rows() { /* return the number of rows affected by the last INSERT, UPDATE, or DELETE * query */ return mysql_affected_rows(); } function db_insert_id() { /* if you just INSERTed a new row into a table with an autonumber, call this * function to give you the ID of the new autonumber value */ return mysql_insert_id(); } function db_free_result($qid) { /* free up the resources used by the query result identifier $qid */ mysql_free_result($qid); } function db_num_fields($qid) { /* return the number of fields returned from the SELECT query with the * identifier $qid */ return mysql_num_fields($qid); } function db_field_name($qid, $fieldno) { /* return the name of the field number $fieldno returned from the SELECT query * with the identifier $qid */ return mysql_field_name($qid, $fieldno); } function db_data_seek($qid, $row) { /* move the database cursor to row $row on the SELECT query with the identifier * $qid */ if (db_num_rows($qid)) { return mysql_data_seek($qid, $row); } } function db_process_query($qryType, $tblName, $fldName, $fldValue, $cndValue, $ordBy) { /* this func basically calls all the required supporting functions stated below to * perform the required task.*/ $this->qryType = $qryType; $this->tblName = $tblName; $this->fldName = $fldName; $this->fldValue = $fldValue; $this->cndValue = $cndValue; $this->ordBy = $ordBy; switch ($this->qryType) { case "select": $this->dbQuery = "select ".$this->fldName; if ($this->tblName != "none") { $this->dbQuery = $this->dbQuery." from ".$this->tblName; } if ($this->cndValue != "none") { $this->dbQuery = $this->dbQuery." where ".$this->cndValue; } if ($this->ordBy != "none") { $this->dbQuery = $this->dbQuery." order by ".$this->ordBy; } break; case "insert": $this->dbQuery = "insert into ".$this->tblName; if ($this->fldName != "none") { $this->dbQuery = $this->dbQuery." ( ".$this->fldName." ) "; } if ($this->fldValue != "none") { $this->dbQuery = $this->dbQuery." values ( ".$this->fldValue." ) "; } break; case "update": $this->dbQuery = "update ".$this->tblName." set "; if ($this->fldName != "none") { $this->dbQuery = $this->dbQuery.$this->fldName; } if ($this->fldValue != "none") { $this->dbQuery = $this->dbQuery.$this->fldValue; } if ($this->cndValue != "none") { $this->dbQuery = $this->dbQuery." where ".$this->cndValue; } break; case "delete": $this->dbQuery = "delete from ".$this->tblName; if ($this->cndValue != "none") { $this->dbQuery = $this->dbQuery." where ".$this->cndValue; } } $this->dbRsltSet = $this->db_query($this->dbQuery); if ($qryType == "select") { $this->dbNumRow = $this->db_num_rows($this->dbRsltSet); $this->dbNumField = $this->db_num_fields($this->dbRsltSet); } else { $this->dbNumRow = $this->db_affected_rows(); } if ($this->qryType == "insert") { $this->dbNewID = $this->db_insert_id(); } } } ?>