PHP Classes

File: MySQL_Driver.php

Recommend this page to a friend!
  Classes of John Luxford  >  Database.php  >  MySQL_Driver.php  >  Download  
File: MySQL_Driver.php
Role: ???
Content type: text/plain
Description: The MySQL driver classes.
Class: Database.php
Author: By
Last change:
Date: 21 years ago
Size: 9,015 bytes
 

Contents

Class file image Download
<?php // // +----------------------------------------------------------------------+ // | Sitellite - Content Management System | // +----------------------------------------------------------------------+ // | Copyright (c) 2001 Simian Systems | // +----------------------------------------------------------------------+ // | This software is released under the Simian Open Software License. | // | Please see the accompanying file OPENLICENSE for licensing details! | // | | // | You should have received a copy of the Simian Open Software License | // | along with this program; if not, write to Simian Systems, | // | 101-314 Broadway, Winnipeg, MB, R3C 0S7, CANADA. The Simian | // | Public License is also available at the following web site | // | address: <http://www.simian.ca/license.php> | // +----------------------------------------------------------------------+ // | Authors: John Luxford <lux@simian.ca> | // +----------------------------------------------------------------------+ // // MySQL_Driver is the Database driver for the MySQL database system. // /*! <package name="MySQL_Driver"> <class name="MySQL_Query" parent="Query" access="public" date="2001-05-29 11:05:37" version="1.0"> <author name="John Luxford" email="lux@simian.ca" url="http://www.simian.ca/" /> <summary>MySQL_Driver is the Database driver for the MySQL database system. MySQL_Query provides all of the MySQL-specific functionality for Query objects. It is accessed through the Database class API.</summary> <example>$q = new MySQL_Query ("select * from table"); if ($q->execute ()) { while ($row = $q->fetch ()) { // do something with $row } $q->free (); } else { // query failed }</example> !*/ class MySQL_Query extends Query { /*! <property name="sql" access="public" type="string"> <summary>Contains the SQL query to be executed.</summary> </property> !*/ var $sql = ""; /*! <property name="result" access="public" type="resource"> <summary>Contains the result identifier for the current execution.</summary> </property> !*/ var $result = ""; /*! <property name="field" access="public" type="string"> <summary>Currently unused.</summary> </property> !*/ var $field = ""; /*! <property name="connection" access="public" type="resource"> <summary>The resource ID for the database connection to be queried.</summary> </property> !*/ var $connection = ""; /*! <method name="MySQL_Query" access="public"> <summary>Constructor method. $sql is the SQL query you wish to execute with this object. $conn is the database connection resource to be queried.</summary> <param name="sql" type="string" /> <param name="conn" type="resource" /> </method> !*/ function MySQL_Query ($sql = "", $conn = "") { $this->Query ($sql, $conn); } /*! <method name="execute" access="public"> <summary>Executes the current SQL query. $values is an array of values to substitute. The syntax for denoting bind_values in the SQL query is a double-questionmark (??). Returns the new SQL query. Note: does not modify the actual $sql property.</summary> <param name="values" type="array" /> <returns type="resource" /> </method> !*/ function execute () { // call bind_values $this->bind_values (func_get_args ()); // query the database $this->result = mysql_query ($this->tmp_sql); return $this->result; } /*! <method name="field" access="public"> <summary>Returns the name of the specified column in the table currently being queried. $num is the column number. Note: Some database systems begin with column 0, while others with 1.</summary> <param name="num" type="integer" /> <returns type="string" /> </method> !*/ function field ($num = 0) { if ($this->result) { return mysql_field_name ($this->result, $num); } else { return 0; } } /*! <method name="rows" access="public"> <summary>Returns the number of rows affected or found by the current query.</summary> <returns type="integer" /> </method> !*/ function rows () { if ($this->result) { return mysql_num_rows ($this->result); } else { return 0; } } /*! <method name="lastid" access="public"> <summary>Returns the row ID generated by the database during the previous SQL insert query.</summary> <returns type="integer" /> </method> !*/ function lastid () { if ($this->result) { return mysql_insert_id (); } else { return 0; } } /*! <method name="fetch" access="public"> <summary>Returns the next row of data from the current query, always in the form of an object, with each column as its properties.</summary> <returns type="object" /> </method> !*/ function fetch () { if ($this->result) { return mysql_fetch_object ($this->result); } else { return 0; } } /*! <method name="free" access="public"> <summary>Tells the database system to let go of its data from the previous query.</summary> </method> !*/ function free () { if ($this->result) { return mysql_free_result ($this->result); } } /*! <method name="begin" access="public"> <summary>Issues a mysql "begin" statement, beginning a transaction.</summary> </method> !*/ function begin () { mysql_query ("begin"); } /*! <method name="commit" access="public"> <summary>Issues a mysql "commit" statement, committing the current transaction.</summary> </method> !*/ function commit () { mysql_query ("commit"); } /*! <method name="rollback" access="public"> <summary>Issues a mysql "rollback" statement, rolling back the current transaction.</summary> </method> !*/ function rollback () { mysql_query ("rollback"); } } /*! </class> <class name="MySQL_Driver" access="public" date="2001-05-29 11:05:37" version="1.0"> <author name="John Luxford" email="lux@simian.ca" url="http://www.simian.ca/" /> <summary>MySQL_Driver provides all of the MySQL-specific functionality for Database objects. It is accessed through the Database class API.</summary> <example>$db = new MySQL_Driver ("name", "host", "user", "pass", 1); if ($db->connection) { // connection worked } else { // connection failed }</example> !*/ class MySQL_Driver { /*! <property name="connection" access="public" type="resource"> <summary>Contains the database connection resource.</summary> </property> !*/ var $connection; /*! <property name="name" access="public" type="string"> <summary>Contains the name of the database being used.</summary> </property> !*/ var $name; /*! <property name="host" access="public" type="string"> <summary>Contains the name of the database host.</summary> </property> !*/ var $host; /*! <property name="user" access="public" type="string"> <summary>Contains the username used to connect to the current database.</summary> </property> !*/ var $user; /*! <property name="pass" access="public" type="string"> <summary>Contains the password used to connect to the current database.</summary> </property> !*/ var $pass; /*! <property name="persistent" access="public" type="boolean"> <summary>A 1 or 0 (true or false, and true by default), specifying whether to establish a persistent connection or not.</summary> </property> !*/ var $persistent; /*! <method name="MySQL_Driver" access="public"> <summary>Constructor method.</summary> <param name="name" type="string" /> <param name="host" type="string" /> <param name="user" type="string" /> <param name="pass" type="string" /> <param name="persistent" type="boolean" default="1" /> </method> !*/ function MySQL_Driver ($name = "", $host = "", $user = "", $pass = "", $persistent = 1) { $this->name = $name; $this->host = $host; $this->user = $user; $this->pass = $pass; $this->persistent = $persistent; $this->connect (); } /*! <method name="connect" access="public"> <summary>Establishes a connection with the database</summary> </method> !*/ function connect () { if ($this->persistent) { $this->connection = mysql_pconnect ($this->host, $this->user, $this->pass); } else { $this->connection = mysql_connect ($this->host, $this->user, $this->pass); } mysql_select_db ($this->name, $this->connection); } /*! <method name="query" access="public"> <summary>Creates and returns a Query object. $sql is the SQL query you wish to execute with this object.</summary> <param name="sql" type="string" /> <returns type="object" /> </method> !*/ function query ($sql = "") { // inherits full functionality from Database class } } /*! </class> </package> !*/ ?>