Source for file mssql.php
Documentation is available at mssql.php
// vim: set et ts=4 sw=4 fdm=marker:
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox, |
// | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | MDB is a merge of PEAR DB and Metabases that provides a unified DB |
// | API as well as database abstraction for PHP applications. |
// | This LICENSE is in the BSD license style. |
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
// | Lukas Smith nor the names of his contributors may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: Frank M. Kromann <frank@kromann.info> |
// +----------------------------------------------------------------------+
// $Id: mssql.php,v 1.6.4.30 2004/04/10 07:57:16 lsmith Exp $
require_once('MDB/Common.php');
* MDB MSSQL Server driver
* - Until at least version 6.5, the only kind of table changes that the
* ALTER TABLE SQL statement of Microsoft SQL server supports is new field
and constraint additions.
* - The driver alterTable method does not implement table or column renaming,
* column definitions changes or column dropping. In the future versions of
* this driver those capabilities may be emulated using other SQL statements
* to recreate the tables with a new definition.
* MDB_Manager_mssql::getTableFieldDefinition() is still alpha quality
* @author Frank M. Kromann <frank@kromann.info>
$this->phptype = 'mssql';
$this->dbsyntax = 'mssql';
$this->supported['Sequences'] = 1;
$this->supported['Indexes'] = 1;
$this->supported['AffectedRows'] = 1;
$this->supported['Transactions'] = 1;
$this->supported['Summaryfunctions'] = 1;
$this->supported['OrderByText'] = 0;
$this->supported['CurrId'] = 0;
$this->supported['SelectRowRanges'] = 1;
$this->supported['LOBs'] = 1;
$this->supported['Replace'] = 1;
$this->supported['SubSelects'] = 1;
$this->errorcode_map = array (
* Get the native error code of the last error (if any) that
* occured on the current connection.
* @return int native FrontBase error code
$res = @mssql_query ('select @@ERROR as ErrorCode', $this->connection);
$row = @mssql_fetch_row ($res);
* This method is used to communicate an error and invoke error
* callbacks etc. Basically a wrapper for MDB::raiseError
* that checks for native error msgs.
* @param string $message userinfo message
* @param integer $errno error code
* @return object a PEAR error object
$error = @mssql_get_last_message ();
* 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
* Define whether database changes done on the database be automatically
* committed. This function may also implicitly start or end a transaction.
* @param boolean $auto_commit flag that indicates whether the database
* changes should be committed right after
* executing every query statement. If this
* argument is 0 a transaction implicitly
* started. Otherwise, if a transaction is
* in progress it is ended by committing any
* database changes that were pending.
* @return mixed MDB_OK on success, a MDB error on failure
$this->debug("AutoCommit: ". ($auto_commit ? "On" : "Off"));
if (!isset ($this->supported['Transactions'])) {
'Auto-commit transactions: transactions are not in use'));
if ($this->auto_commit == $auto_commit) {
$result = $this->query('COMMIT TRANSACTION');
$result = $this->query('BEGIN TRANSACTION');
$this->auto_commit = $auto_commit;
$this->in_transaction = !$auto_commit;
* Commit the database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after committing the pending changes.
* @return mixed MDB_OK on success, a MDB error on failure
$this->debug("Commit Transaction");
if (!isset ($this->supported['Transactions'])) {
'Commit transactions: transactions are not in use'));
if ($this->auto_commit) {
'Commit transactions: transaction changes are being auto commited'));
$result = $this->query('COMMIT TRANSACTION');
return($this->query('BEGIN TRANSACTION'));
* Cancel any database changes done during a transaction that is in
* progress. This function may only be called when auto-committing is
* disabled, otherwise it will fail. Therefore, a new transaction is
* implicitly started after canceling the pending changes.
* @return mixed MDB_OK on success, a MDB error on failure
$this->debug("Rollback Transaction");
if (!isset ($this->supported['Transactions'])) {
'Rollback transactions: transactions are not in use'));
if ($this->auto_commit) {
'Rollback transactions: transactions can not be rolled back when changes are auto commited'));
$result = $this->query('ROLLBACK TRANSACTION');
return($this->query('BEGIN TRANSACTION'));
function _doQuery ($query)
$this->current_row = $this->affected_rows = -1;
* Connect to the database
* @return TRUE on success, MDB_Error on failure
$port = (isset ($this->port) ? $this->port : '');
$this->affected_rows = -1;
if(!PEAR ::loadExtension ($this->phptype)) {
NULL , NULL , 'extension '. $this->phptype. ' is not compiled into PHP',
$function = ($this->options['persistent'] ? 'mssql_pconnect' : 'mssql_connect');
$this->host. (!strcmp($port,'') ? '' : ':'. $port),
$this->user, $this->password);
if(isset ($this->supported['Transactions']) && !$this->auto_commit
&& !$this->_doQuery ("BEGIN TRANSACTION"))
$this->affected_rows = -1;
return($this->raiseError("Connect: Could not begin the initial transaction"));
$this->selected_database = $this->database_name;
* all the RDBMS specific things needed close a DB connection
if (isset ($this->supported['Transactions']) && !$this->auto_commit) {
$result = $this->_doQuery ("ROLLBACK TRANSACTION");
$this->affected_rows = $this->current_row = -1;
unset ($GLOBALS['_MDB_databases'][$this->database]);
if(!PEAR ::loadExtension ($this->phptype)) {
NULL , NULL , 'extension '. $this->phptype. ' is not compiled into PHP',
$connection = @mssql_connect ($this->host,$this->user,$this->password);
return($this->mssqlRaiseError(NULL , "Query: Could not connect to the Microsoft SQL server"));
$result = @mssql_query ($query, $connection);
return($this->mssqlRaiseError(NULL , "Query: Could not query a Microsoft SQL server"));
@mssql_close ($connection);
* Send a query to the database and return any results
* @param string $query the SQL query
* @param mixed $types array that contains the types of the columns in
* @return mixed a result handle or MDB_OK on success, a MDB error on failure
function query($query, $types = NULL )
$this->debug(" Query: $query" );
if ($this->database_name) {
$this->last_query = $query;
$first = $this->first_selected_row;
$limit = $this->selected_row_limit;
$this->first_selected_row = $this->selected_row_limit = 0;
$fetch = $first + $limit;
$query = str_replace('SELECT', " SELECT TOP $fetch" , $query);
|| !strcmp($this->selected_database, '')
|| strcmp($this->selected_database, $this->database_name))
if(!@mssql_select_db ($this->database_name, $this->connection)) {
if ($result = $this->_doQuery ($query)) {
$this->affected_rows = @mssql_rows_affected ($this->connection);
$result_value = intval($result);
if($first > 0 || $limit > 0 ) {
$this->limits[$result_value] = array ($first, $limit);
* Retrieve the names of columns returned by the DBMS in a query result.
* @param resource $result result identifier
* @return mixed an associative array variable
* that will hold the names of columns. The
* indexes of the array are the column names
* mapped to lower case and the values are the
* respective numbers of the columns starting
* from 0. Some DBMS may not return any
* columns when the result set does not
$result_value = intval($result);
'Get column names: it was specified an inexisting result set'));
if (!isset ($this->columns[$result_value])) {
$this->columns[$result_value] = array ();
$columns = @mssql_num_fields ($result);
for($column = 0; $column < $columns; $column++ ) {
$field_name = @mssql_field_name ($result, $column);
if ($this->options['optimize'] == 'portability') {
$this->columns[$result_value][$field_name] = $column;
return($this->columns[$result_value]);
* Count the number of columns returned by the DBMS in a query result.
* @param resource $result result identifier
* @return mixed integer value with the number of columns, a MDB error
'numCols: it was specified an inexisting result set'));
return(@mssql_num_fields ($result));
* check if the end of the result set has been reached
* @param resource $result result identifier
* @return mixed TRUE or FALSE on sucess, a MDB error on failure
$result_value = intval($result);
'End of result: attempted to check the end of an unknown result'));
* fetch value from a result set
* @param resource $result result identifier
* @param int $row number of the row where the data can be found
* @param int $field field number where the data can be found
* @return mixed string on success, a MDB error on failure
function fetch($result, $row, $field)
$result_value = intval($result);
if (isset ($this->limits[$result_value])) {
$row += $this->limits[$result_value][0 ];
$res = @mssql_result ($result, $row, $field);
if ($res === FALSE && $res != NULL ) {
* fetch a clob value from a result set
* @param resource $result result identifier
* @param int $row number of the row where the data can be found
* @param int $field field number where the data can be found
* @return mixed content of the specified data cell, a MDB error on failure,
return($this->fetchLob($result, $row, $field));
* fetch a blob value from a result set
* @param resource $result result identifier
* @param int $row number of the row where the data can be found
* @param int $field field number where the data can be found
* @return mixed content of the specified data cell, a MDB error on failure
return($this->fetchLob($result, $row, $field));
* convert a value to a RDBMS indepdenant MDB type
* @param mixed $value value to be converted
* @param int $type constant that specifies which type to convert to
* @return mixed converted value
return ($value == '1') ? TRUE : FALSE;
return($this->_baseConvertResult ($value,$type));
* returns the number of rows in a result object
* @param ressource $result a valid result ressouce pointer
* @return mixed MDB_Error or the number of rows
$result_value = intval($result);
$rows = @mssql_num_rows ($result);
if (isset ($this->limits[$result_value])) {
$rows -= $this->limits[$result_value][0 ];
if ($rows < 0 ) $rows = 0;
* Free the internal resources associated with $result.
* @param $result result identifier
* @return boolean TRUE on success, FALSE if $result is invalid
$result_value = intval($result);
if(isset ($this->fetched_row[$result_value])) {
unset ($this->fetched_row[$result_value]);
if(isset ($this->columns[$result_value])) {
unset ($this->columns[$result_value]);
if(isset ($this->result_types[$result_value])) {
unset ($this->result_types[$result_value]);
return(@mssql_free_result ($result));
// {{{ getIntegerDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an integer type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Boolean flag that indicates whether the field
* should be declared as unsigned integer if
* Integer value to be used as default for this
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
if (isset ($field['unsigned'])) {
$this->warnings[] = " unsigned integer field \"$name\" is being
declared as signed integer";
return(" $name INT". (isset ($field["default"]) ? " DEFAULT ". $field["default"] : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getTextDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an text type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Integer value that determines the maximum length of the text
* field. If this argument is missing the field should be
* declared to have the longest length allowed by the DBMS.
* Text value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return((isset ($field["length"]) ? " $name VARCHAR (". $field["length"]. ")" : " $name TEXT" ). (isset ($field["default"]) ? " DEFAULT ". $this->GetTextValue ($field["default"]) : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getClobDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an character
* large object type field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the
* properties of the field being declared as array
* indexes. Currently, the types of supported field
* properties are as follows:
* Integer value that determines the maximum length
* of the large object field. If this argument is
* missing the field should be declared to have the
* longest length allowed by the DBMS.
* Boolean flag that indicates whether this field
* is constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
if (isset ($field["length"])) {
$length = $field["length"];
$type = " VARCHAR($length)";
return(" $name $type". (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getBlobDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an binary large
* object type field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Integer value that determines the maximum length
* of the large object field. If this argument is
* missing the field should be declared to have the
* longest length allowed by the DBMS.
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
if(isset ($field["length"])) {
$length = $field["length"];
$type = " VARBINARY($length)";
return(" $name $type". (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getBooleanDeclaration()
* Obtain DBMS specific SQL code portion needed to declare a boolean type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes. Currently, the types
* of supported field properties are as follows:
* Boolean value to be used as default for this field.
* Boolean flag that indicates whether this field is constrained
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name BIT". (isset ($field["default"]) ? " DEFAULT ". $field["default"] : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getDateDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an date type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field properties
* Date value to be used as default for this field.
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name CHAR (". strlen("YYYY-MM-DD"). ")". (isset ($field["default"]) ? " DEFAULT ". $this->getDateValue($field["default"]) : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getTimestampDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an timestamp
* type field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Time stamp value to be used as default for this
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name CHAR (". strlen("YYYY-MM-DD HH:MM:SS"). ")". (isset ($field["default"]) ? " DEFAULT ". $this->getTimestampValue($field["default"]) : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getTimeDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an time type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Time value to be used as default for this field.
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name CHAR (". strlen("HH:MM:SS"). ")". (isset ($field["default"]) ? " DEFAULT ". $this->getTimeValue($field["default"]) : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getFloatDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an float type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Integer value to be used as default for this
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name FLOAT". (isset ($field["default"]) ? " DEFAULT ". $field["default"] : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
// {{{ getDecimalDeclaration()
* Obtain DBMS specific SQL code portion needed to declare an decimal type
* field to be used in statements like CREATE TABLE.
* @param string $name name the field to be declared.
* @param string $field associative array with the name of the properties
* of the field being declared as array indexes.
* Currently, the types of supported field
* properties are as follows:
* Integer value to be used as default for this
* Boolean flag that indicates whether this field is
* constrained to not be set to NULL.
* @return string DBMS specific SQL code portion that should be used to
* declare the specified field.
return(" $name DECIMAL(18,". $this->decimal_places. ")". (isset ($field["default"]) ? " DEFAULT ". $this->getDecimalValue($field["default"]) : ""). (isset ($field["notnull"]) ? " NOT NULL" : " NULL"));
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param resource $prepared_query query handle from prepare()
* @return string text string that represents the given argument value in
* a DBMS specific format.
if (MDB::isError($result = $this->readLob($clob, $data, $this->options['lob_buffer_length']))) {
$value .= $this->_quote ($data);
* free a character large object
* @param resource $prepared_query query handle from prepare()
unset ($this->lobs[$clob]);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param resource $prepared_query query handle from prepare()
* @return string text string that represents the given argument value in
* a DBMS specific format.
if (MDB::isError($result = $this->readLob($blob, $data, $this->options['lob_buffer_length']))) {
* free a binary large object
* @param resource $prepared_query query handle from prepare()
unset ($this->lobs[$blob]);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @return string text string that represents the given argument value in
* a DBMS specific format.
return(($value === NULL ) ? 'NULL' : $value);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @return string text string that represents the given argument value in
* a DBMS specific format.
return(($value === NULL ) ? 'NULL' : $value);
* Convert a text value into a DBMS specific format that is suitable to
* compose query statements.
* @param string $value text string value that is intended to be converted.
* @return string text string that represents the given argument value in
* a DBMS specific format.
return(($value === NULL ) ? 'NULL' : $value);
* returns the next free id of a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true the seqence is
* automatic created, if it
* @return mixed MDB_Error or id
function nextId($seq_name, $ondemand = TRUE )
$result = $this->query(" INSERT INTO $sequence_name DEFAULT VALUES" );
// Since we are creating the sequence on demand
// we know the first id = 1 so initialize the
'Next ID: on demand sequence could not be created'));
// First ID of a newly created sequence is 1
$value = $this->queryOne(" SELECT @@IDENTITY FROM $sequence_name" , 'integer');
$result = $this->query(" DELETE FROM $sequence_name WHERE ". $this->options['sequence_col_name']." < $value" );
$this->warnings[] = 'nextID: could not delete previous sequence table values';
* Fetch a row and insert the data into an existing array.
* @param resource $result result identifier
* @param int $fetchmode how the array data should be indexed
* @param int $rownum the row number to fetch
* @return int data array on success, a MDB error on failure
function fetchInto($result, $fetchmode = MDB_FETCHMODE_DEFAULT , $rownum = NULL )
$result_value = intval($result);
if (isset ($this->limits[$result_value])) {
$rownum = $rownum + $this->limits[$result_value][0 ];
if (!@mssql_data_seek ($result, $rownum)) {
$fetchmode = $this->fetchmode;
$row = @mssql_fetch_assoc ($result);
if (is_array($row) && $this->options['optimize'] == 'portability') {
$row = @mssql_fetch_row ($result);
if($this->options['autofree']) {
if (isset ($this->result_types[$result_value])) {
* Returns information about a table or a result set
* NOTE: doesn't support table name and flags if called from a db_result
* @param mixed $resource SQL Server result identifier or table name
* @param int $mode A valid tableInfo mode (MDB_TABLEINFO_ORDERTABLE or
* @return array An array with all the information
* depending on $mode, metadata returns the following values:
* - mode is false (default):
* [0]['table'] table name
* [0]['len'] field length
* [0]['flags'] field flags
* - mode is MDB_TABLEINFO_ORDER
* ["num_fields"] number of metadata records
* [0]['table'] table name
* [0]['len'] field length
* [0]['flags'] field flags
* ['order'][field name] index of field named "field name"
* The last one is used, if you have a field name, but no index.
* Test: if (isset($result['meta']['myfield'])) { ...
* - mode is MDB_TABLEINFO_ORDERTABLE
* the same as above. but additionally
* ["ordertable"][table name][field name] index of field
* this is, because if you have fields from different
* tables with the same field name * they override each
* other with MDB_TABLEINFO_ORDER
* you can combine MDB_TABLEINFO_ORDER and
* MDB_TABLEINFO_ORDERTABLE with MDB_TABLEINFO_ORDER |
* MDB_TABLEINFO_ORDERTABLE * or with MDB_TABLEINFO_FULL
// if $result is a string, then we want information about a
// table without a resultset
if (!@mssql_select_db ($this->database_name, $this->connection)) {
$id = @mssql_query (" SELECT * FROM $result" , $this->connection);
} else { // else we want information about a resultset
$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'] = (is_string($result)) ? $result : '';
$res[$i]['name'] = @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'] = is_string($result) ? $this->_mssql_field_flags ($result, $res[$i]['name']) : '';
$res['num_fields']= $count;
for ($i=0; $i< $count; $i++ ) {
$res[$i]['table'] = (is_string($result)) ? $result : '';
$res[$i]['name'] = @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'] = is_string($result) ? $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
// {{{ _mssql_field_flags()
* Get the flags for a field, currently only supports "isnullable" and "primary_key"
* @param string The table name
* @param string The field
function _mssql_field_flags ($table, $column)
static $current_table = NULL;
// At the first call we discover the flags for all fields
if ($table != $current_table) {
$q_nulls = " SELECT syscolumns.name, syscolumns.isnullable
INNER JOIN syscolumns ON sysobjects.id = syscolumns.id
WHERE sysobjects.name ='$table' AND syscolumns.isnullable = 1";
$res = $this->query($q_nulls, NULL , FALSE );
foreach ($res as $data) {
if ($data['isnullable'] == 1 ) {
$flags[$data['name']][] = 'isnullable';
$res2 = $this->query(" EXEC SP_PKEYS[$table]" , NULL , FALSE );
foreach ($res2 as $data) {
if (!empty ($data['COLUMN_NAME'])) {
$flags[$data['COLUMN_NAME']][] = 'primary_key';
if (isset ($flags[$column])) {
return(implode(',', $flags[$column]));
Documentation generated on Mon, 11 Mar 2019 10:15:27 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|