Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class db {
- var $last_error; // holds the last error. Usually mysql_error()
- var $last_query; // holds the last query executed.
- var $row_count; // holds the last number of rows from a select
- var $host; // mySQL host to connect to
- var $user; // mySQL user name
- var $pw; // mySQL password
- var $db; // mySQL database to select
- var $db_link; // current/last database link identifier
- var $auto_slashes; // add/strip slashes when it can
- function __construct() {
- // class constructor. Initializations here.
- // You can also set these values in the connect() function and using
- // the select_database() function.
- $this->host = 'localhost';
- $this->user = 'PhilW';
- $this->pw = 'PhilW';
- $this->db = 'PhilW';
- $this->auto_slashes = true;
- $this->connect();
- }
- function connect( $host='', $user='', $pw='', $db='', $persistant = true ) {
- // Opens a connection to MySQL and selects the database. If any of the
- // function's parameter's are set, we want to update the class variables.
- // If they are NOT set, then we're going to use the currently existing
- // class variables.
- // Returns true if successful, false if there is failure.
- if ( !empty( $host ) ) $this->host = $host;
- if ( !empty( $user ) ) $this->user = $user;
- if ( !empty( $pw ) ) $this->pw = $pw;
- // Establish the connection.
- if ( $persistant ) {
- $this->db_link = mysql_pconnect( $this->host, $this->user, $this->pw );
- } else {
- $this->db_link = mysql_connect( $this->host, $this->user, $this->pw );
- }
- // Check for an error establishing a connection
- if (!$this->db_link) {
- $this->last_error = mysql_error();
- return false;
- }
- // Select the database
- if ( !$this->select_db( $db ) ) {
- return false;
- } else {
- return $this->db_link; // success
- }
- }
- function select_db( $db = '' ) {
- // Selects the database for use. If the function's $db parameter is
- // passed to the function then the class variable will be updated.
- if ( !empty( $db ) ) $this->db = $db;
- if ( !mysql_select_db( $this->db ) ) {
- $this->last_error = mysql_error();
- return false;
- } else {
- return true;
- }
- }
- function select($sql) {
- // Performs an SQL query and returns the result pointer or false
- // if there is an error.
- $this->last_query = $sql;
- $r = mysql_query( $sql );
- if ( !$r ) {
- $this->last_error = mysql_error();
- return false;
- } else {
- $this->row_count = mysql_num_rows($r);
- return $r;
- }
- }
- function get_row( $result, $type = 'MYSQL_BOTH' ) {
- // Returns a row of data from the query result. You would use this
- // function in place of something like while($row=mysql_fetch_array($r)).
- // Instead you would have while($row = $db->get_row($r)) The
- // main reason you would want to use this instead is to utilize the
- // auto_slashes feature.
- if ( !$result ) {
- $this->last_error = "Invalid resource identifier passed to get_row() function.";
- return false;
- }
- if ( $type == 'MYSQL_ASSOC' ) $row = mysql_fetch_array( $result, MYSQL_ASSOC );
- if ( $type == 'MYSQL_NUM' ) $row = mysql_fetch_array( $result, MYSQL_NUM );
- if ( $type == 'MYSQL_BOTH' ) $row = mysql_fetch_array( $result, MYSQL_BOTH );
- if (!$row) return false;
- if ($this->auto_slashes) {
- // strip all slashes out of row...
- foreach ($row as $key => $value) {
- $row[$key] = stripslashes($value);
- }
- } else {
- return $row;
- }
- }
- function insert_sql($sql) {
- // Inserts data in the database via SQL query.
- // Returns the id of the insert or true if there is not auto_increment
- // column in the table. Returns false if there is an error.
- $this->last_query = $sql;
- $r = mysql_query( $sql );
- if ( !$r ) {
- $this->last_error = mysql_error();
- return false;
- }
- $id = mysql_insert_id();
- if ($id == 0) {
- return true;
- } else {
- return $id;
- }
- }
- function update_sql($sql) {
- // Updates data in the database via SQL query.
- // Returns the number or row affected or true if no rows needed the update.
- // Returns false if there is an error.
- $this->last_query = $sql;
- $r = mysql_query( $sql );
- if ( !$r ) {
- $this->last_error = mysql_error();
- return false;
- }
- $rows = mysql_affected_rows();
- if ($rows == 0){
- return true; // no rows were updated
- } else {
- return $rows;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement