Source for file msql.php
Documentation is available at msql.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* The PEAR DB driver for PHP's msql extension
* for interacting with Mini SQL databases
* PHP's mSQL extension did weird things with NULL values prior to PHP
* 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: msql.php,v 1.64 2007/09/21 13:40:41 aharvey Exp $
* @link http://pear.php.net/package/DB
* Obtain the DB_common class so it can be extended from
require_once 'DB/common.php';
* The methods PEAR DB uses to interact with PHP's msql extension
* for interacting with Mini SQL databases
* These methods overload the ones declared in DB_common.
* PHP's mSQL extension did weird things with NULL values prior to PHP
* 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
* @author Daniel Convissor <danielc@php.net>
* @copyright 1997-2007 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 1.7.14RC1
* @link http://pear.php.net/package/DB
* @since Class not functional until Release 1.7.0
* The DB driver type (mysql, oci8, odbc, etc.)
* The database syntax variant to be used (db2, access, etc.), if any
* The capabilities of this DB implementation
* The 'new_link' element contains the PHP version that first provided
* new_link support for this DBMS. Contains false if it's unsupported.
* Meaning of the 'limit' element:
* + 'emulate' = emulate with fetch row by number
* + 'alter' = alter the query
* A mapping of native error codes to DB error codes
* The raw database connection created by PHP
* The DSN information for connecting to a database
* The query result resource created by PHP
* Used to make affectedRows() work. Only contains the result for
* data manipulation queries. Contains false for other queries.
* This constructor calls <kbd>$this->DB_common()</kbd>
* Connect to the database server, log in and open the database
* Don't call this method directly. Use DB::connect() instead.
* Example of how to connect:
* // $dsn = 'msql://hostname/dbname'; // use a TCP connection
* $dsn = 'msql:///dbname'; // use a socket
* 'portability' => DB_PORTABILITY_ALL,
* $db = DB::connect($dsn, $options);
* if (PEAR::isError($db)) {
* die($db->getMessage());
* @param array $dsn the data source name
* @param bool $persistent should the connection be persistent?
* @return int DB_OK on success. A DB_Error object on failure.
function connect($dsn, $persistent = false )
if (!PEAR ::loadExtension ('msql')) {
? $dsn['hostspec'] . ',' . $dsn['port']
$connect_function = $persistent ? 'msql_pconnect' : 'msql_connect';
if (($err = @msql_error ()) != '') {
if (!@msql_select_db ($dsn['database'], $this->connection)) {
* Disconnects from the database server
* @return bool TRUE on success, FALSE on failure
* Sends a query to the database server
* @param string the SQL query string
* @return mixed + a PHP result resrouce for successful SELECT queries
* + the DB_OK constant for other successful queries
* + a DB_Error object on failure
// Determine which queries that should return data, and which
// should return an error code only.
$this->_result = $result;
* Move the internal msql result pointer to the next available result
* @param a valid fbsql result resource
* @return true if a result is available otherwise return false
* Places a row from the result set into the given array
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* This method is not meant to be called directly. Use
* DB_result::fetchInto() instead. It can't be declared "protected"
* because DB_result is a separate object.
* PHP's mSQL extension did weird things with NULL values prior to PHP
* 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
* @param resource $result the query result resource
* @param array $arr the referenced array to put the data in
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch (0 = first row)
* @return mixed DB_OK on success, NULL when the end of a result set is
* @see DB_result::fetchInto()
function fetchInto($result, &$arr, $fetchmode, $rownum = null )
if (!@msql_data_seek ($result, $rownum)) {
$arr = @msql_fetch_array ($result, MSQL_ASSOC );
$arr = @msql_fetch_row ($result);
* Deletes the result set and frees the memory occupied by the result set
* This method is not meant to be called directly. Use
* DB_result::free() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result PHP's query result resource
* @return bool TRUE on success, FALSE if $result is invalid
return is_resource($result) ? msql_free_result ($result) : false;
* Gets the number of columns in a result set
* This method is not meant to be called directly. Use
* DB_result::numCols() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result PHP's query result resource
* @return int the number of columns. A DB_Error object on failure.
* @see DB_result::numCols()
$cols = @msql_num_fields ($result);
* Gets the number of rows in a result set
* This method is not meant to be called directly. Use
* DB_result::numRows() instead. It can't be declared "protected"
* because DB_result is a separate object.
* @param resource $result PHP's query result resource
* @return int the number of rows. A DB_Error object on failure.
* @see DB_result::numRows()
$rows = @msql_num_rows ($result);
* Determines the number of rows affected by a data maniuplation query
* 0 is returned for queries that don't manipulate data.
* @return int the number of rows. A DB_Error object on failure.
return msql_affected_rows ($this->_result);
* Returns the next free id in a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true, the seqence is automatically
* created if it does not exist
* @return int the next id number in the sequence.
* A DB_Error object on failure.
* @see DB_common::nextID(), DB_common::getSequenceName(),
* DB_msql::createSequence(), DB_msql::dropSequence()
function nextId($seq_name, $ondemand = true )
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$result = $this->query(" SELECT _seq FROM ${seqname}" );
$this->popErrorHandling ();
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$this->popErrorHandling ();
* Also creates a new table to associate the sequence with. Uses
* a separate table to ensure portability with other drivers.
* @param string $seq_name name of the new sequence
* @return int DB_OK on success. A DB_Error object on failure.
* @see DB_common::createSequence(), DB_common::getSequenceName(),
* DB_msql::nextID(), DB_msql::dropSequence()
$res = $this->query('CREATE TABLE ' . $seqname
. ' (id INTEGER NOT NULL)');
$res = $this->query(" CREATE SEQUENCE ON ${seqname}" );
* @param string $seq_name name of the sequence to be deleted
* @return int DB_OK on success. A DB_Error object on failure.
* @see DB_common::dropSequence(), DB_common::getSequenceName(),
* DB_msql::nextID(), DB_msql::createSequence()
* mSQL does not support delimited identifiers
* @param string $str the identifier name to be quoted
* @return object a DB_Error object
* @see DB_common::quoteIdentifier()
* @since Method available since Release 1.7.0
* Formats a float value for use within a query in a locale-independent
* @param float the float value to be quoted.
* @return string the quoted string.
* @see DB_common::quoteSmart()
* @since Method available since release 1.7.8.
* Escapes a string according to the current DBMS's standards
* @param string $str the string to be escaped
* @return string the escaped string
* @see DB_common::quoteSmart()
* @since Method available since Release 1.7.0
* Produces a DB_Error object regarding the current problem
* @param int $errno if the error is being manually raised pass a
* DB_ERROR* constant here. If this isn't passed
* the error information gathered from the DBMS.
* @return object the DB_Error object
* @see DB_common::raiseError(),
* DB_msql::errorNative(), DB_msql::errorCode()
return $this->raiseError($errno, null , null , null , $native);
* Gets the DBMS' native error message produced by the last query
* @return string the DBMS' error message
* Determines PEAR::DB error code from the database's text error message
* @param string $errormsg the error message returned from the database
* @return integer the error number from a DB_ERROR* constant
// PHP 5.2+ prepends the function name to $php_errormsg, so we need
// this hack to work around it, per bug #9599.
$errormsg = preg_replace('/^msql[a-z_]+\(\): /', '', $errormsg);
if (!isset ($error_regexps)) {
'/^Access to database denied/i'
'/^Bad type for comparison/i'
'/^Can\'t perform LIKE on/i'
'/^Can\'t use TEXT fields in LIKE comparison/i'
'/^Couldn\'t create temporary table/i'
'/^Error creating table file/i'
'/^Field .* cannot be null$/i'
'/^Index (field|condition) .* cannot be null$/i'
'/^Invalid date format/i'
'/^Invalid time format/i'
'/^Literal value for .* is wrong type$/i'
'/^No Database Selected/i'
'/^No value specified for field/i'
'/^Non unique value for unique index/i'
'/^Out of memory for temporary table/i'
'/^Reference to un-selected table/i'
'/^Unknown (index|system variable)/i'
|