Source for file mssql.php
Documentation is available at mssql.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Sterling Hughes <sterling@php.net> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
// $Id: mssql.php,v 1.53 2004/03/05 01:46:53 danielc Exp $
require_once 'DB/common.php';
* Database independent query interface definition for PHP's Microsoft SQL Server
* @version $Id: mssql.php,v 1.53 2004/03/05 01:46:53 danielc Exp $
* @author Sterling Hughes <sterling@php.net>
// XXX Add here error codes ie: 'S100E' => DB_ERROR_SYNTAX
function connect($dsninfo, $persistent = false )
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
$dbhost .= $dsninfo['port'] ? ':' . $dsninfo['port'] : '';
$connect_function = $persistent ? 'mssql_pconnect' : 'mssql_connect';
if ($dbhost && $dsninfo['username'] && $dsninfo['password']) {
$conn = @$connect_function($dbhost, $dsninfo['username'],
} elseif ($dbhost && $dsninfo['username']) {
$conn = @$connect_function($dbhost, $dsninfo['username']);
$conn = @$connect_function($dbhost);
null , @mssql_get_last_message ());
if ($dsninfo['database']) {
if (!@mssql_select_db ($dsninfo['database'], $conn)) {
null , @mssql_get_last_message ());
$this->_db = $dsninfo['database'];
if (!@mssql_select_db ($this->_db, $this->connection)) {
$query = $this->modifyQuery ($query);
$result = @mssql_query ('BEGIN TRAN', $this->connection);
$result = @mssql_query ($query, $this->connection);
// Determine which queries that should return data, and which
// should return an error code only.
return $ismanip ? DB_OK : $result;
* Move the internal mssql result pointer to the next available result
* @param a valid fbsql result resource
* @return true if a result is available otherwise return false
return @mssql_next_result ($result);
* Fetch a row and insert the data into an existing array.
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* @param resource $result query result identifier
* @param array $arr (reference) array where data from the row
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch
* @return mixed DB_OK on success, null when end of result set is
* @see DB_result::fetchInto()
function fetchInto ($result, &$arr, $fetchmode, $rownum=null )
if (!@mssql_data_seek ($result, $rownum)) {
$arr = @mssql_fetch_array ($result, MSSQL_ASSOC );
$arr = @mssql_fetch_row ($result);
/* This throws informative error messages,
if ($msg = @mssql_get_last_message()) {
return $this->raiseError($msg);
$this->_rtrimArrayValues ($arr);
$this->_convertNullArrayValuesToEmpty ($arr);
return @mssql_free_result ($result);
$cols = @mssql_num_fields ($result);
$rows = @mssql_num_rows ($result);
* Enable/disable automatic commits
// XXX if $this->transaction_opcount > 0, we should probably
* Commit the current transaction.
if (!@mssql_select_db ($this->_db, $this->connection)) {
$result = @mssql_query ('COMMIT TRAN', $this->connection);
* Roll back (undo) the current transaction.
if (!@mssql_select_db ($this->_db, $this->connection)) {
$result = @mssql_query ('ROLLBACK TRAN', $this->connection);
* Gets the number of rows affected by the last query.
* if the last query was a select, returns 0.
* @return number of rows affected by the last query or DB_ERROR
$res = @mssql_query ('select @@rowcount', $this->connection);
$ar = @mssql_fetch_row ($res);
@mssql_free_result ($res);
* 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. DB_Error if problem.
* @see DB_common::nextID()
function nextId($seq_name, $ondemand = true )
$seqname = $this->getSequenceName ($seq_name);
if (!@mssql_select_db ($this->_db, $this->connection)) {
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$result = $this->query(" INSERT INTO $seqname (vapor) VALUES (0)" );
$this->popErrorHandling ();
$result = & $this->query(" SELECT @@IDENTITY FROM $seqname" );
* @param string $seq_name name of the new sequence
* @return int DB_OK on success. A DB_Error object is returned if
* @see DB_common::createSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" CREATE TABLE $seqname ".
'([id] [int] IDENTITY (1, 1) NOT NULL ,' .
* @param string $seq_name name of the sequence to be deleted
* @return int DB_OK on success. DB_Error if problems.
* @see DB_common::dropSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" DROP TABLE $seqname" );
* Determine MS SQL Server error code by querying @@ERROR.
* @return mixed mssql's native error code or DB_ERROR if unknown.
$res = @mssql_query ('select @@ERROR as ErrorCode', $this->connection);
$row = @mssql_fetch_row ($res);
* Determine PEAR::DB error code from mssql's native codes.
* If <var>$nativecode</var> isn't known yet, it will be looked up.
* @param mixed $nativecode mssql error code, if known
* @return integer an error number from a DB error constant
* Gather information about an error, then use that info to create a
* DB error object and finally return that object.
* @param integer $code PEAR error number (usually a DB constant) if
* manually raising an error
* @return object DB error object
* @see DB_common::raiseError()
$message = @mssql_get_last_message ();
* Returns information about a table or a result set.
* NOTE: only supports 'table' and 'flags' if <var>$result</var>
* @param object|string $result DB_result object from a query or a
* string containing the name of a table
* @param int $mode a valid tableInfo mode
* @return array an associative array with the information requested
* or an error object if something is wrong
* @see DB_common::tableInfo()
if (isset ($result->result )) {
* Probably received a result object.
* Extract the result resource identifier.
* Probably received a table name.
* Create a result resource identifier.
if (!@mssql_select_db ($this->_db, $this->connection)) {
$id = @mssql_query (" SELECT * FROM $result WHERE 1=0" ,
* Probably received a result resource identifier.
* Depricated. Here for compatibility only.
$case_func = 'strtolower';
$count = @mssql_num_fields ($id);
// made this IF due to performance (one if is faster than $count if's)
for ($i=0; $i< $count; $i++ ) {
$res[$i]['table'] = $got_string ? $case_func($result) : '';
$res[$i]['name'] = $case_func(@mssql_field_name ($id, $i));
$res[$i]['type'] = @mssql_field_type ($id, $i);
$res[$i]['len'] = @mssql_field_length ($id, $i);
// We only support flags for tables
$res[$i]['flags'] = $got_string ? $this->_mssql_field_flags ($result, $res[$i]['name']) : '';
$res['num_fields']= $count;
for ($i=0; $i< $count; $i++ ) {
$res[$i]['table'] = $got_string ? $case_func($result) : '';
$res[$i]['name'] = $case_func(@mssql_field_name ($id, $i));
$res[$i]['type'] = @mssql_field_type ($id, $i);
$res[$i]['len'] = @mssql_field_length ($id, $i);
// We only support flags for tables
$res[$i]['flags'] = $got_string ? $this->_mssql_field_flags ($result, $res[$i]['name']) : '';
$res['order'][$res[$i]['name']] = $i;
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
// free the result only if we were called on a table
* Returns the query needed to get some backend info
* @param string $type What kind of info you want to retrieve
* @return string The SQL query string
return "select name from sysobjects where type = 'U' order by name";
return "select name from sysobjects where type = 'V'";
// {{{ _mssql_field_flags()
* Get the flags for a field, currently supports "not_null", "primary_key",
* "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
* "unique_key" (mssql unique index, unique check or primary_key) and
* "multiple_key" (multikey index)
* mssql timestamp is NOT similar to the mysql timestamp so this is maybe
* not useful at all - is the behaviour of mysql_field_flags that primary
* keys are alway unique? is the interpretation of multiple_key correct?
* @param string The table name
* @param string The field
* @author Joern Barthel <j_barthel@web.de>
function _mssql_field_flags ($table, $column)
static $tableName = null;
if ($table != $tableName) {
// get unique and primary keys
$res = $this->getAll (" EXEC SP_HELPINDEX[$table]" , DB_FETCHMODE_ASSOC );
$keys = explode(', ', $val['index_keys']);
foreach ($keys as $key) {
$this->_add_flag ($flags[$key], 'multiple_key');
if (strpos($val['index_description'], 'primary key')) {
foreach ($keys as $key) {
$this->_add_flag ($flags[$key], 'primary_key');
} elseif (strpos($val['index_description'], 'unique')) {
foreach ($keys as $key) {
$this->_add_flag ($flags[$key], 'unique_key');
// get auto_increment, not_null and timestamp
if ($val['nullable'] == '0') {
$this->_add_flag ($flags[$val['column_name']], 'not_null');
if (strpos($val['type_name'], 'identity')) {
$this->_add_flag ($flags[$val['column_name']], 'auto_increment');
if (strpos($val['type_name'], 'timestamp')) {
$this->_add_flag ($flags[$val['column_name']], 'timestamp');
if (array_key_exists ($column, $flags)) {
return(implode (' ', $flags[$column]));
* Adds a string to the flags array if the flag is not yet in there
* - if there is no flag present the array is created.
* @param reference Reference to the flag-array
* @param value The flag value
* @author Joern Barthel <j_barthel@web.de>
function _add_flag (&$array, $value)
* Quote a string so it can be safely used as a table / column name
* Quoting style depends on which database driver is being used.
* @param string $str identifier name to be quoted
* @return string quoted identifier string
Documentation generated on Mon, 11 Mar 2019 10:14:53 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|