Mega Code Archive

 
Categories / Php / MySQL Database
 

MySQL driver class

<?php /** * A "driver" for mySQL databases * Contains code for connecting, inserting, selecting and * generally doing lots of stuff to the database. By doing * this you have an easy, single point of access for dealing * with mySQL functions, and can also count the number of queries * used per instance of this class. It has let me tidy up my code * no end. * To use it, instantiate the class and call ::db_connect(), eg: * $sql = new sql_driver(); * $sql->db_connect(); */ class sql_driver { /** * Connection details array */ var $conn_details = array( 'db_db' => 'DATABASE_NAME', 'db_user' => 'USER_NAME', 'db_pass' => 'PASSWORD', 'db_host' => 'localhost', 'db_port' => '', ); /** * Whether or not to use persistant connections (1=yes, 0=no) */ var $persistant = 0; /** * Connection identifier */ var $conn = ''; /** * Result identifier */ var $result = ''; /** * Number of queries performed */ var $query_count = 0; /** * Result row */ var $result_row = array(); /** * Connects to the database * * @return void */ function db_connect() { if ($this->persistant) { $this->conn = @mysql_pconnect( $this->conn_details['db_host'], $this->conn_details['db_user'], $this->conn_details['db_pass']); } else { $this->conn = @mysql_connect( $this->conn_details['db_host'], $this->conn_details['db_user'], $this->conn_details['db_pass']); } if (!$this->conn) { echo 'Error: unable to connect to database server.'; } if (!@mysql_select_db($this->conn_details['db_db'], $this->conn)) { echo 'Error: unable to connect to specified database.'; } } /** * Carries out query * * @return resource mySQL result resource */ function query($query) { $this->result = mysql_query($query, $this->conn); $this->query_count++; return $this->result; } /** * Returns an associative array of each row * * Returns an associative array of a row from the result in parameters. If no * parameter is present, it returns a row from the last result. * * @return array An array of the fetched row. */ function get_row() { $this->result_row = array(); $this->result_row = mysql_fetch_assoc($this->result); return $this->result_row; } /** * Returns the number of rows in a result * * @return integer Number of rows in the result. */ function get_num_rows() { return mysql_num_rows($this->result); } /** * Returns the number of rows affected by the last insert, update or delete query. * * The PHP manual says that this will not be reliable if the updated value is the * same as the pre-update value. * * @return integer The number of affected rows. */ function get_affected_rows() { return mysql_affected_rows($this->conn); } /** * Returns the number of queries performed. * * @return integer The number of queries performed. */ function get_query_count() { return $this->query_count; } /** * Frees the result resource from memory */ function free_result() { mysql_free_result($this->result); } /** * Closes the mySQL connection */ function db_close() { mysql_close($this->conn); } } // End sql_driver class ?>