header image
 

PHP MySQL Class

Here you go :)
As normal feel free to use but mail me where you do :)

You will have to add your details to the global variables to connect of course :)

<?
/**
 * Class:sql
 * + Handles all the sql queries.
 * --------
 * @author MDalke
 * @copyright 2007 MDalke
 * @todo + Constants from constants class.
 *       + Proper Labeling of what everything does
 */
class sql {
 var $conn_id;
 var $result;
 var $record;
 var $db = array();
 var $host;
 var $user;
 var $pass;
 var $dbname;
 var $constant;
 var $debugStack = array();
 /**
     * void sql
     * + Class Construct (PHP4)
  * -----------------
  * @param string $host
  * @param string $user
  * @param string $pass
  * @param string $dbname
  * @return sql
  */
 function sql($host=null,$user=null,$pass=null,$dbname=null){
  $this->__construct($host=null,$user=null,$pass=null,$dbname=null);
 }
    /**
     * void __construct
     * + Class Construct fix for PHP5
  * -----------------
     * @param string $host
     * @param string $user
     * @param string $pass
     * @param string $dbname
     * @return __construct
     */
 function __construct($host=null,$user=null,$pass=null,$dbname=null) {
  $this->host = ($host)?$host:$this->host;
  $this->user = ($user)?$user:$this->user;
  $this->pass = ($pass)?$pass:$this->pass;
  $this->dbname = ($dbname)?$dbname:$this->dbname;
 }
 
 /**
  * void connect
  *  + Connects to the mysql
  * -----------------
  * @return void
  */
 function connect() {
        // + Using the getconst function to get the required details
        // + Needed:
        //    + Host (The DB Host)
        //    + User (The DB User)
        //    + Pass (The DB pass)
        //    + Dbname (The DB Name)
       
  $this->host = $dbhost;
  $this->user = $dbuser;
  $this->pass = $dbpasswd;
  $this->dbname = $dbname;
        // + Opens the connection to the DB
  mysql_connect($this->host,$this->user,$this->pass);
  // + Selects the required database or dies.
  @mysql_select_db($this->dbname) or die("Unable to select database");
 }
 /**
  * string query
  * + Executes the query to the MYSQL.
  * -----------------
  * @param string $query_string
  * @return string $result
  */
 function query($query_string) {
  $this->result = mysql_query($query_string);
  return $this->result;
 }
 /**
  * array fetch_array
  * + Creates an array of results
  * -----------------
  * @param string $query_id
  * @return array $record
  */
 function fetch_array($query_id) {
  $this->record = mysql_fetch_array($query_id,MYSQL_ASSOC);
  return $this->record;
 }
 /**
  * int num_rows
  * + Gets number of results
  * -----------------
  * @param string $query_id
  * @return int
  */
 function num_rows($query_id) {
  return ($query_id) ? mysql_num_rows($query_id) : 0;
 }
   
 /**
  * int num_fields
  * + Gets number of fields in the results
  * -----------------
  * @param string $query_id
  * @return int
  */ 
 function num_fields($query_id) {
  return ($query_id) ? mysql_num_fields($query_id) : 0;
 }
 /**
  * void free_result
  * + Frees the result set.
  * -----------------
  * @param string $query_id
  * @return void
  */
 function free_result($query_id) {
  return mysql_free_result($query_id);
 }
 /**
  * int affected rows
  * + Get number of affected rows in previous MySQL operation
  * -----------------
  * @return int
  */
 function affected_rows() {
  return mysql_affected_rows($this->conn_id);
 }
 /**
  * boolean close_db
  * + Closes the connection to the DB
  * -----------------
  * @return boolean
  */
 function close_db() {
  if($this->conn_id) {
   return mysql_close($this->conn_id);
  } else {
   return false;
  }
 }
 /**
  * Void debug
  * + Creates a set of debug instructions
  * -----------------
  * @param string $msg
  */
 function debug($msg){
  $this->debugStack .= $msg."<br>\n";
 }
 /**
  * String returnDebug
  * + Returns the debugstack.
  * -----------------
  * @return string $debugStack
  */
 function returnDebug(){
  return $this->debugStack;
 }
}
?>

~ by mdalke on October 19, 2007.

One Response to “PHP MySQL Class”

  1. I’ll test it in my php project.

Leave a Reply