Microsoft SQL Server

目录

Warning

This feature was REMOVED in PHP 7.0.0.

Alternatives to this feature include:

These functions allow you to access MS SQL Server database.

This extension is not available anymore on Windows with PHP 5.3 or later.

SQLSRV, an alternative extension for MS SQL connectivity is available from Microsoft: » http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx.

安装/配置

目录

需求

Requirements for Win32 platforms.

The extension requires the MS SQL Client Tools to be installed on the system where PHP is installed. The Client Tools can be installed from the MS SQL Server CD or by copying ntwdblib.dll from \winnt\system32 on the server to \winnt\system32 on the PHP box. Copying ntwdblib.dll will only provide access through named pipes. Configuration of the client will require installation of all the tools.

This extension is not available anymore on Windows with PHP 5.3 or later.

SqlSrv, an alternative driver for MS SQL is available from Microsoft: » http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx.

Requirements for Unix/Linux platforms.

To use the MSSQL extension on Unix/Linux, you first need to build and install the FreeTDS library. Source code and installation instructions are available at the FreeTDS home page: » http://www.freetds.org/

Note:

On Windows, the DBLIB from Microsoft is used. Functions that return a column name are based on the dbcolname() function in DBLIB. DBLIB was developed for SQL Server 6.x where the max identifier length is

  1. For this reason, the maximum column length is 30 characters. On platforms where FreeTDS is used (Linux), this is not a problem.

Note:

On Windows, if you're using MSSQL 2005 or greater you must copy the ntwdblib.dll into the directory where you have installed php and overwrite the one thats already in there. This is due to the version distributed is old and outdated. Alternatively you can use the » http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx, ODBC, PDO_DBLIB or PDO_ODBC extensions, to talk to MSSQL.

安装

The MSSQL extension is enabled by adding extension=php_mssql.dll to php.ini.

To get these functions to work, you have to compile PHP with --with-mssql[=DIR], where DIR is the FreeTDS install prefix. And FreeTDS should be compiled using --enable-msdblib.

Warning

MS SQL functions are aliases to Sybase functions if PHP is compiled with Sybase extension and without MS SQL extension.

运行时配置

这些函数的行为受 php.ini 中的设置影响。

名字默认可修改范围更新日志
mssql.allow_persistent"1"PHP_INI_SYSTEM 
mssql.max_persistent"-1"PHP_INI_SYSTEM 
mssql.max_links"-1"PHP_INI_SYSTEM 
mssql.min_error_severity"10"PHP_INI_ALL 
mssql.min_message_severity"10"PHP_INI_ALL 
mssql.compatibility_mode"0"PHP_INI_ALL 
mssql.connect_timeout"5"PHP_INI_ALL 
mssql.timeout"60"PHP_INI_ALLAvailable since PHP 4.1.0.
mssql.textsize"-1"PHP_INI_ALL 
mssql.textlimit"-1"PHP_INI_ALL 
mssql.batchsize"0"PHP_INI_ALLAvailable since PHP 4.0.4.
mssql.datetimeconvert"1"PHP_INI_ALLAvailable since PHP 4.2.0.
mssql.secure_connection"0"PHP_INI_SYSTEMAvailable since PHP 4.3.0.
mssql.max_procs"-1"PHP_INI_ALLAvailable since PHP 4.3.0.
mssql.charset""PHP_INI_ALLAvailable since PHP 5.1.2 when built with FreeTDS 7.0 or greater.

有关 PHP_INI_* 样式的更多详情与定义,见 配置可被设定范围

资源类型

mssql link

A connection link returned by mssql_connect and mssql_pconnect.

mssql result

A result handle returned by mssql_query on SELECT queries.

mssql statement

A statement handle returned by mssql_init.

预定义常量

下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。

MSSQL_ASSOC (integer)
Return an associative array. Used on mssql_fetch_array's result_type parameter.

MSSQL_NUM (integer)
Return an array with numeric keys. Used on mssql_fetch_array's result_type parameter.

MSSQL_BOTH (integer)
Return an array with both numeric keys and keys with their field name. This is the default value for mssql_fetch_array's result_type parameter.

SQLTEXT (integer)
Indicates the 'TEXT' type in MSSQL, used by mssql_bind's type parameter.

SQLVARCHAR (integer)
Indicates the 'VARCHAR' type in MSSQL, used by mssql_bind's type parameter.

SQLCHAR (integer)
Indicates the 'CHAR' type in MSSQL, used by mssql_bind's type parameter.

SQLINT1 (integer)
Represents one byte, with a range of -128 to 127.

SQLINT2 (integer)
Represents two bytes, with a range of -32768 to

SQLINT4 (integer)
Represents four bytes, with a range of -2147483648 to 2147483647.

SQLBIT (integer)
Indicates the 'BIT' type in MSSQL, used by mssql_bind's type parameter.

SQLFLT4 (integer)
Represents an four byte float.

SQLFLT8 (integer)
Represents an eight byte float.

mssql_bind

Adds a parameter to a stored procedure or a remote stored procedure

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::bindParam
  • PDOStatement::bindValue
  • sqlsrv_prepare
  • odbc_prepare

说明

bool mssql_bind ( resource $stmt , string $param_name , mixed &$var , int $type [, bool $is_output = FALSE [, bool $is_null = FALSE [, int $maxlen = -1 ]]] )

Binds a parameter to a stored procedure or a remote stored procedure.

参数

stmt
Statement resource, obtained with mssql_init.

param_name
The parameter name, as a string.

Note:

You have to include the @ character, like in the T-SQL syntax. See the explanation included in class="function">mssql_execute.

var
The PHP variable you'll bind the MSSQL parameter to. It is passed by reference, to retrieve OUTPUT and RETVAL values after the procedure execution.

type
One of: SQLTEXT, SQLVARCHAR, SQLCHAR, SQLINT1, SQLINT2, SQLINT4, SQLBIT, SQLFLT4, SQLFLT8, SQLFLTN.

is_output
Whether the value is an OUTPUT parameter or not. If it's an OUTPUT parameter and you don't mention it, it will be treated as a normal input parameter and no error will be thrown.

is_null
Whether the parameter is NULL or not. Passing the NULL value as var will not do the job.

maxlen
Used with char/varchar values. You have to indicate the length of the data so if the parameter is a varchar(50), the type must be SQLVARCHAR and this value 50.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_bind example

<?php
// Connect to MSSQL and select the database
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Create a new stored prodecure
$stmt = mssql_init('NewUserRecord');

// Bind the field names
mssql_bind($stmt, '@username',  'Kalle',  SQLVARCHAR,  false,  false,  60);
mssql_bind($stmt, '@name',      'Kalle',  SQLVARCHAR,  false,  false,  60);
mssql_bind($stmt, '@age',       19,       SQLINT1,     false,  false,   3);

// Execute
mssql_execute($stmt);

// Free statement
mssql_free_statement($stmt);
?>

参见

  • mssql_execute
  • mssql_free_statement
  • mssql_init

mssql_close

关闭MS SQL Server链接

说明

bool mssql_close ([ resource $link_identifier ] )

关闭指定的链接标识符相关联的MS SQL Server数据库链接。如果链接不存在,则试图关闭最后产生的链接。

注:这个操作通常不是必要的,在非持久打开的链接在程序执行结束后会自动关闭链接。

参数

link_identifier
一个SQL Server链接, mssql_connect 的返回值。

这个方法不能关闭由mssql_pconnect 产生的长链接。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_close 例子

<?php
// Connect to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');

// Do any related operations here

// Close the link to MSSQL
mssql_close($link);
?>

参见

  • mssql_connect
  • mssql_pconnect

mssql_connect

打开MS SQL server链接

说明

resource mssql_connect ([ string $servername [, string $username [, string $password [, bool $new_link = false ]]]] )

mssql_connect 建立一个MS SQL server链接。

链接会在程序执行结束之后关闭, 除非在之前已经通过调用mssql_close关闭。

参数

servername
服务器,它可以包含一个端口号 e.g. 主机:端口号(hostname:port) (Linux), 或 主机,端口号(hostname,port) (Windows).

username
用户名。

password
密码。

new_link
如果第二次调用mssql_connect使用相同的参数, 不会建立新的链接,而是将之前已经打开的链接标识返回。对这个参数的值进行修改,则使得调用 mssql_connect总是会建立新的链接, 即便调用mssql_connect使用与之前同样的参数。

返回值

返回一个链接视为成功, 或者 FALSE 和错误信息.

更新日志

版本说明
5.1.0new_link参数加入

范例

示例 #1 mssql_connect 例子

<?php
// Server in the this format: <computer>\<instance name> or 
// <server>,<port> when using a non default port number
$server = 'KALLESPC\SQLEXPRESS';

// Connect to MSSQL
$link = mssql_connect($server, 'sa', 'phpfi');

if (!$link) {
    die('Something went wrong while connecting to MSSQL');
}
?>

参见

  • mssql_close
  • mssql_pconnect

mssql_data_seek

Moves internal row pointer

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

说明

bool mssql_data_seek ( resource $result_identifier , int $row_number )

mssql_data_seek moves the internal row pointer of the MS SQL result associated with the specified result identifier to point to the specified row number, first row being number

  1. The next call to mssql_fetch_row would return that row.

参数

result_identifier
The result resource that is being evaluated.

row_number
The desired row number of the new result pointer.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_data_seek example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Select all people
$result = mssql_query('SELECT [name], [age] FROM [persons] WHERE [age] >= 13');

if (!$result) {
    die('Query failed.');
}

// Select every 4th student in the results
for ($i = mssql_num_rows($result) - 1; $i % 4; $i++) {
    if (!mssql_data_seek($result, $i)) {
        continue;
    }

    // Fetch the row ...
}

// Free the query result
mssql_free_result($result);
?>

mssql_execute

Executes a stored procedure on a MS SQL server database

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

说明

mixed mssql_execute ( resource $stmt [, bool $skip_results = FALSE ] )

Executes a stored procedure on a MS SQL server database

参数

stmt
Statement handle obtained with mssql_init.

skip_results
Whenever to skip the results or not.

范例

示例 #1 mssql_execute example

<?php
// Create a new statement
$stmt = mssql_init('NewBlogEntry');

// Some data strings
$title = 'Test of blogging system';
$content = 'If you can read this, then the new system is compatible with MSSQL';

// Bind values
mssql_bind($stmt, '@author',    'Felipe Pena',  SQLVARCHAR,     false,  false,   60);
mssql_bind($stmt, '@date',      '08/10/2008',   SQLVARCHAR,     false,  false,   20);
mssql_bind($stmt, '@title',     $title,         SQLVARCHAR,     false,  false,   60);
mssql_bind($stmt, '@content',   $content,       SQLTEXT);

// Execute the statement
mssql_execute($stmt);

// And we can free it like so:
mssql_free_statement($stmt);
?>

注释

Note:

If the stored procedure returns parameters or a return value these will be available after the call to class="function">mssql_execute unless the stored procedure returns more than one result set. In that case use class="function">mssql_next_result to shift through the results. When the last result has been processed the output parameters and return values will be available.

参见

  • mssql_bind
  • mssql_free_statement
  • mssql_init

mssql_fetch_array

Fetch a result row as an associative array, a numeric array, or both

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::fetch
  • sqlsrv_fetch_array
  • odbc_fetch_array

说明

array mssql_fetch_array ( resource $result [, int $result_type = MSSQL_BOTH ] )

mssql_fetch_array is an extended version of mssql_fetch_row. In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.

An important thing to note is that using mssql_fetch_array is NOT significantly slower than using mssql_fetch_row, while it provides a significant added value.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

result_type
The type of array that is to be fetched. It's a constant and can take the following values: MSSQL_ASSOC, MSSQL_NUM, and MSSQL_BOTH.

返回值

Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.

范例

示例 #1 mssql_fetch_array example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username], [name] FROM [php].[dbo].[userlist]');

// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found';
} else {
    // The following is equal to the code below:
    //
    // while ($row = mssql_fetch_row($query)) {

    while ($row = mssql_fetch_array($query, MSSQL_NUM)) {
        // ...
    }
}

// Free the query result
mssql_free_result($query);
?>

注释

Note: class="simpara">此函数返回的字段名大小写敏感

Note: 此函数将 NULL 字段设置为 PHP NULL 值。

参见

  • mssql_fetch_row

mssql_fetch_assoc

Returns an associative array of the current row in the result

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::fetch
  • sqlsrv_fetch_array
  • odbc_fetch_array

说明

array mssql_fetch_assoc ( resource $result_id )

Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mssql_fetch_assoc is equivalent to calling mssql_fetch_array with MSSQL_ASSOC for the optional second parameter.

参数

result_id
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns an associative array that corresponds to the fetched row, or FALSE if there are no more rows.

范例

示例 #1 mssql_fetch_assoc example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username], [name] FROM [php].[dbo].[userlist]');

// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found';
}
else
{
    // Print a nice list of users in the format of:
    // * name (username)

    echo '<ul>';

    while ($row = mssql_fetch_assoc($query)) {
        echo '<li>' . $row['name'] . ' (' . $row['username'] . ')</li>';
    }

    echo '</ul>';
}

// Free the query result
mssql_free_result($query);
?>

mssql_fetch_batch

Returns the next batch of records

Warning

This function was REMOVED in PHP 7.0.0.

说明

int mssql_fetch_batch ( resource $result )

Returns the next batch of records.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns the number of rows in the returned batch.

范例

示例 #1 mssql_fetch_batch example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Send a query
$result = mssql_query('SELECT * FROM [php].[dbo].[people]', $link, 100);
$records = 10;

while ($records >= 0) {
    while ($row = mssql_fetch_assoc($result)) {
        // Do stuff ...
    }

    --$records;
}

if ($batchsize = mssql_fetch_batch($result)) {
    // $batchsize is the number of records left 
    // in the result, but not shown above
}
?>

mssql_fetch_field

Get field information

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::getColumnMeta
  • sqlsrv_field_metadata
  • The various odbc_field_* functions in the unified ODBC driver.

说明

object mssql_fetch_field ( resource $result [, int $field_offset = -1 ] )

mssql_fetch_field can be used in order to obtain information about fields in a certain query result.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

field_offset
The numerical field offset. If the field offset is not specified, the next field that was not yet retrieved by this function is retrieved. The field_offset starts at 0.

返回值

Returns an object containing field information.

The properties of the object are:

  • name - column name. if the column is a result of a function, this property is set to computed#N, where #N is a serial number.
  • column_source - the table from which the column was taken
  • max_length - maximum length of the column
  • numeric - 1 if the column is numeric
  • type - the column type.

范例

示例 #1 mssql_fetch_field example

<?php
// Connect to MSSQL and select the database
mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Send a select query to MSSQL
$query = mssql_query('SELECT * FROM [php].[dbo].[persons]');

// Construct table
echo '<h3>Table structure for \'persons\'</h3>';
echo '<table border="1">';

// Table header
echo '<thead>';
echo '<tr>';
echo '<td>Field name</td>';
echo '<td>Data type</td>';
echo '<td>Max length</td>';
echo '</tr>';
echo '</thead>';

// Dump all fields
echo '<tbody>';

for ($i = 0; $i < mssql_num_fields($query); ++$i) {
    // Fetch the field information
    $field = mssql_fetch_field($query, $i);

    // Print the row
    echo '<tr>';
    echo '<td>' . $field->name . '</td>';
    echo '<td>' . strtoupper($field->type) . '</td>';
    echo '<td>' . $field->max_length . '</td>';
    echo '</tr>';
}

echo '</tbody>';
echo '</table>';

// Free the query result
mssql_free_result($query);
?>

参见

  • mssql_field_seek

mssql_fetch_object

Fetch row as object

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::fetchObject
  • sqlsrv_fetch_object
  • odbc_fetch_object

说明

object mssql_fetch_object ( resource $result )

mssql_fetch_object is similar to mssql_fetch_array, with one difference - an object is returned, instead of an array. Indirectly, that means that you can only access the data by the field names, and not by their offsets (numbers are illegal property names).

Speed-wise, the function is identical to mssql_fetch_array, and almost as quick as mssql_fetch_row (the difference is insignificant).

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows.

范例

示例 #1 mssql_fetch_object example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username], [name] FROM [php].[dbo].[userlist]');

// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found';
} else {
    // Print a nice list of users in the format of:
    // * name (username)

    echo '<ul>';

    while ($row = mssql_fetch_object($query)) {
        echo '<li>' . $row->name . ' (' . $row->username . ')</li>';
    }

    echo '</ul>';
}

// Free the query result
mssql_free_result($query);
?>

注释

Note: class="simpara">此函数返回的字段名大小写敏感

Note: 此函数将 NULL 字段设置为 PHP NULL 值。

参见

  • mssql_fetch_array
  • mssql_fetch_row

mssql_fetch_row

Get row as enumerated array

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::fetch
  • sqlsrv_fetch_array
  • odbc_fetch_row

说明

array mssql_fetch_row ( resource $result )

mssql_fetch_row fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

Subsequent call to mssql_fetch_row would return the next row in the result set, or FALSE if there are no more rows.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.

范例

示例 #1 mssql_fetch_row example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Query to execute
$query = mssql_query('SELECT [id], [quote] FROM [quotes] WHERE [id] = \'42\'', $link);

// Did the query fail?
if (!$query) {
    die('MSSQL error: ' . mssql_get_last_message());
}

// Fetch the row
$row = mssql_fetch_row($query);

// Print the 'quote'
echo 'Quote #' . $row[0] . ': "' . $row[1] . '"';
?>

以上例程的输出类似于:

Quote #42: "The answer to everything..."

注释

Note: 此函数将 NULL 字段设置为 PHP NULL 值。

参见

  • mssql_fetch_array
  • mssql_fetch_object
  • mssql_data_seek
  • mssql_result

mssql_field_length

Get the length of a field

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::getColumnMeta
  • sqlsrv_field_metadata
  • odbc_field_len

说明

int mssql_field_length ( resource $result [, int $offset = -1 ] )

Returns the length of field no. offset in result.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

offset
The field offset, starts at 0. If omitted, the current field is used.

返回值

The length of the specified field index on success 或者在失败时返回 FALSE.

范例

示例 #1 mssql_field_length example

<?php
// Connect to MSSQL and select the database
mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Send a select query to MSSQL
$query = mssql_query('SELECT [name], [age] FROM [php].[dbo].[persons]');

// Print the field length
echo 'The field \'age\' has a data length of ' . mssql_field_length($query, 1);

// Free the query result
mssql_free_result($query);
?>

以上例程的输出类似于:

The field 'age' has a data length of 4

注释

Note: Note to Windows Users

Due to a limitation in the underlying API used by PHP (MS DBLib C API), the length of VARCHAR fields is limited to 255. If you need to store more data, use a TEXT field instead.

参见

  • mssql_field_name
  • mssql_field_type

mssql_field_name

Get the name of a field

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::getColumnMeta
  • sqlsrv_field_metadata
  • odbc_field_name

说明

string mssql_field_name ( resource $result [, int $offset = -1 ] )

Returns the name of field no. offset in result.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

offset
The field offset, starts at 0. If omitted, the current field is used.

返回值

The name of the specified field index on success 或者在失败时返回 FALSE.

范例

示例 #1 mssql_field_name example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username], [name], [email] FROM [php].[dbo].[userlist]');

echo 'Result set contains the following field(s):', PHP_EOL;

// Dump all field names in result
for ($i = 0; $i < mssql_num_fields($query); ++$i) {
    echo ' - ' . mssql_field_name($query, $i), PHP_EOL;
}

// Free the query result
mssql_free_result($query);
?>

以上例程的输出类似于:

Result set contains the following field(s):
 - username
 - name
 - email

参见

  • mssql_field_length
  • mssql_field_type

mssql_field_seek

Seeks to the specified field offset

Warning

This function was REMOVED in PHP 7.0.0.

说明

bool mssql_field_seek ( resource $result , int $field_offset )

Seeks to the specified field offset. If the next call to mssql_fetch_field won't include a field offset, this field would be returned.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

field_offset
The field offset, starts at 0.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 Using mssql_field_seek on the example for mssql_fetch_field

<?php
// Connect to MSSQL and select the database
mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Send a select query to MSSQL
$query = mssql_query('SELECT * FROM [php].[dbo].[persons]');

// Construct table
echo '<h3>Table structure for \'persons\'</h3>';
echo '<table border="1">';

// Table header
echo '<thead>';
echo '<tr>';
echo '<td>Field name</td>';
echo '<td>Data type</td>';
echo '<td>Max length</td>';
echo '</tr>';
echo '</thead>';

// Dump all fields
echo '<tbody>';

for ($i = 0; $i < mssql_num_fields($query); ++$i) {
    // Fetch the field information, notice the 
    // field_offset parameter is not set. See 
    // the mssql_field_seek call below
    $field = mssql_fetch_field($query);

    // Print the row
    echo '<tr>';
    echo '<td>' . $field->name . '</td>';
    echo '<td>' . strtoupper($field->type) . '</td>';
    echo '<td>' . $field->max_length . '</td>';
    echo '</tr>';

    // Move the internal seek pointer to the next
    // row in the result set
    mssql_field_seek($query, $i + 1);
}

echo '</tbody>';
echo '</table>';

// Free the query result
mssql_free_result($query);
?>

参见

  • mssql_fetch_field

mssql_field_type

Gets the type of a field

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::getColumnMeta
  • sqlsrv_field_metadata
  • odbc_field_type

说明

string mssql_field_type ( resource $result [, int $offset = -1 ] )

Returns the type of field no. offset in result.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

offset
The field offset, starts at 0. If omitted, the current field is used.

返回值

The type of the specified field index on success 或者在失败时返回 FALSE.

范例

示例 #1 mssql_field_type example

<?php
// Connect to MSSQL and select the database
mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Send a select query to MSSQL
$query = mssql_query('SELECT [name] FROM [php].[dbo].[persons]');

// Print the field type and length
echo '\'' . mssql_field_name($query, 0) . '\' is a type of ' . 
     strtoupper(mssql_field_type($query, 0)) . 
     '(' . mssql_field_length($query, 0) . ')';

// Free the query result
mssql_free_result($query);
?>

以上例程的输出类似于:

'name' is a type of CHAR(50)

参见

  • mssql_field_length
  • mssql_field_name

mssql_free_result

Free result memory

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • odbc_free_result

说明

bool mssql_free_result ( resource $result )

mssql_free_result only needs to be called if you are worried about using too much memory while your script is running. All result memory will automatically be freed when the script ends. You may call mssql_free_result with the result identifier as an argument and the associated result memory will be freed.

参数

result
The result resource that is being freed. This result comes from a call to mssql_query.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_free_result example

<?php
// Select some data from a table
$query = mssql_query('SELECT * FROM [php].[dbo].[persons]', $link);

// Handle query result here

// When we're done we free the result by calling
// mssql_free_result like so:
mssql_free_result($query);
?>

参见

  • mssql_free_statement

mssql_free_statement

Free statement memory

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • sqlsrv_free_stmt

说明

bool mssql_free_statement ( resource $stmt )

mssql_free_statement only needs to be called if you are worried about using too much memory while your script is running. All statement memory will automatically be freed when the script ends. You may call mssql_free_statement with the statement identifier as an argument and the associated statement memory will be freed.

参数

stmt
Statement resource, obtained with mssql_init.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_free_statement example

<?php
// Create a new statement
$stmt = mssql_init('test');

// Bind values here and execute the statement

// once we're done, we clear it from the memory
// using mssql_free_statement like so:
mssql_free_statement($stmt);
?>

参见

  • mssql_bind
  • mssql_execute
  • mssql_init
  • mssql_free_result

mssql_get_last_message

Returns the last message from the server

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::errorInfo
  • sqlsrv_errors
  • odbc_errormsg

说明

string mssql_get_last_message ( void )

Gets the last message from the MS-SQL server

参数

此函数没有参数。

返回值

Returns last error message from server, or an empty string if no error messages are returned from MSSQL.

范例

示例 #1 mssql_get_last_message example

<?php
// Connect to MSSQL and select the database
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Make a query that will fail
$query = @mssql_query('SELECT * FROM [php].[dbo].[not-found]');

if (!$query) {
    // The query has failed, print a nice error message
    // using mssql_get_last_message()
    die('MSSQL error: ' . mssql_get_last_message());
}
?>

以上例程的输出类似于:

MSSQL error: Invalid object name 'php.dbo.not-found'.

参见

  • mssql_min_error_severity
  • mssql_min_message_severity

mssql_guid_string

Converts a 16 byte binary GUID to a string

Warning

This function was REMOVED in PHP 7.0.0.

说明

string mssql_guid_string ( string $binary [, bool $short_format = FALSE ] )

Converts a 16 byte binary GUID to a string.

参数

binary
A 16 byte binary GUID.

short_format
Whenever to use short format.

返回值

Returns the converted string on success.

范例

示例 #1 mssql_guid_string example

<?php
$binary = '19555081977808608437941339997619274330352755554827939936';

var_dump(mssql_guid_string($binary));
var_dump(mssql_guid_string($binary, true));
?>

以上例程会输出:

string(36) "35353931-3035-3138-3937-373830383630"
string(32) "31393535353038313937373830383630"

mssql_init

Initializes a stored procedure or a remote stored procedure

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

说明

resource mssql_init ( string $sp_name [, resource $link_identifier ] )

Initializes a stored procedure or a remote stored procedure.

参数

sp_name
Stored procedure name, like ownew.sp_name or otherdb.owner.sp_name.

link_identifier
A MS SQL link identifier, returned by mssql_connect.

返回值

Returns a resource identifier "statement", used in subsequent calls to mssql_bind and mssql_execute, or FALSE on errors.

范例

示例 #1 mssql_init example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Create a new statement
$stmt = mssql_init('StatementTest', $link);

// Bind values here

// Once values are binded we execute our statement 
// using mssql_execute:
mssql_execute($stmt);

// And we can free it like so:
mssql_free_statement($stmt);
?>

参见

  • mssql_bind
  • mssql_execute
  • mssql_free_statement

mssql_min_error_severity

Sets the minimum error severity

Warning

This function was REMOVED in PHP 7.0.0.

说明

void mssql_min_error_severity ( int $severity )

Sets the minimum error severity.

参数

severity
The new error severity.

返回值

没有返回值。

范例

示例 #1 mssql_min_error_severity example

<?php
// Connect to MSSQL and select the database
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Set the minimum error severity to not include SQL 
// syntax errors by setting it to something greater than 
// or equal to 1.
mssql_min_error_severity(1);

// Send a query we know that will cause an syntax error, in
// this case we use the MySQL quote signs instead of wrapping 
// square brackets around the field and table names.
$query = mssql_query('SELECT `syntax`, `error` FROM `MSSQL`');

if (!$query) {
    // Custom error handler ...
}
?>

mssql_min_message_severity

Sets the minimum message severity

Warning

This function was REMOVED in PHP 7.0.0.

说明

void mssql_min_message_severity ( int $severity )

Sets the minimum message severity.

参数

severity
The new message severity.

返回值

没有返回值。

范例

示例 #1 mssql_min_message_severity example

<?php
// Connect to MSSQL
mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');

// Set the minimum message severity to 17, this
// will not show any messages issued by the underlaying
// API when we select a non-existent database below
mssql_min_message_severity(17);

// Select a non-existent database
mssql_select_db('THIS_DATABASE_DOES_NOT_EXISTS');
?>

以上例程会输出:

mssql_select_db(): Unable to select database:  THIS_DATABASE_DOES_NOT_EXISTS

mssql_next_result

Move the internal result pointer to the next result

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::nextRowset
  • sqlsrv_next_result
  • odbc_next_result

说明

bool mssql_next_result ( resource $result_id )

When sending more than one SQL statement to the server or executing a stored procedure with multiple results, it will cause the server to return multiple result sets. This function will test for additional results available form the server. If an additional result set exists it will free the existing result set and prepare to fetch the rows from the new result set.

参数

result_id
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns TRUE if an additional result set was available or FALSE otherwise.

范例

示例 #1 mssql_next_result example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Send a query to MSSQL
$sql = 'SELECT [name], [age] FROM [php].[dbo].[persons]';
$query = mssql_query($sql, $link);

// Iterate through returned records
do {
    while ($row = mssql_fetch_row($query)) {
        // Handle record ...
    }
} while (mssql_next_result($query));

// Clean up
mssql_free_result($query);
mssql_close($link);
?>

mssql_num_fields

Gets the number of fields in result

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::columnCount
  • sqlsrv_num_fields
  • odbc_num_fields

说明

int mssql_num_fields ( resource $result )

mssql_num_fields returns the number of fields in a result set.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns the number of fields, as an integer.

范例

示例 #1 mssql_num_fields example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link);

// Select some data from our database
$data = mssql_query('SELECT [name], [age] FROM [php].[dbo].[persons]');

// Construct a table
echo '<table border="1">';

$header = false;

// Iterate through returned results
while ($row = mssql_fetch_array($data)) {
    // Build the table header
    if (!$header) {
        echo '<thead>';
        echo '<tr>';

        for ($i = 1; ($i + 1) <= mssql_num_fields($data); ++$i) {
            echo '<td>' . ucfirst($row[$i]) . '</td>';
        }

        echo '</tr>';
        echo '</thead>';
        echo '<tbody>';

        $header = true;
    }

    // Build the row
    echo '<tr>';

    foreach($row as $value) {
        echo '<td>' . $value . '</td>';
    }

    echo '</tr>';
}

// Close table
echo '</tbody>';
echo '</table>';

// Clean up
mssql_free_result($data);
mssql_close($link);
?>

参见

  • mssql_query
  • mssql_fetch_field
  • mssql_num_rows

mssql_num_rows

Gets the number of rows in result

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::rowCount
  • sqlsrv_num_rows
  • odbc_num_rows

说明

int mssql_num_rows ( resource $result )

mssql_num_rows returns the number of rows in a result set.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

返回值

Returns the number of rows, as an integer.

范例

示例 #1 mssql_num_rows example

<?php
// Connect to MSSQL and select the database
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php');

// Select all our records from a table
$query = mssql_query('SELECT * FROM [php].[dbo].[persons]');

echo 'Total records in database: ' . mssql_num_rows($query);

// Clean up
mssql_free_result($query);
?>

参见

  • mssql_query
  • mssql_fetch_row

mssql_pconnect

Open persistent MS SQL connection

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDO::__construct
  • sqlsrv_connect
  • odbc_pconnect

说明

resource mssql_pconnect ([ string $servername [, string $username [, string $password [, bool $new_link = FALSE ]]]] )

mssql_pconnect acts very much like mssql_connect with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mssql_close will not close links established by mssql_pconnect).

This type of links is therefore called 'persistent'.

参数

servername
The MS SQL server. It can also include a port number. e.g. hostname:port.

username
The username.

password
The password.

new_link
If a second call is made to mssql_pconnect with the same arguments, no new link will be established, but instead, the link identifier of the already opened link will be returned. This parameter modifies this behavior and makes mssql_pconnect always open a new link, even if mssql_pconnect was called before with the same parameters.

返回值

Returns a positive MS SQL persistent link identifier on success, or FALSE on error.

范例

示例 #1 mssql_pconnect using the new_link parameter

<?php
// Connect to MSSQL and select the database
$link1 = mssql_pconnect('MANGO\SQLEXPRESS', 'sa', 'phpfi');
mssql_select_db('php', $link1);

// Create a new link
$link2 = mssql_pconnect('MANGO\SQLEXPRESS', 'sa', 'phpfi', true);
mssql_select_db('random', $link2);
?>

mssql_query

Send MS SQL query

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDO::query
  • sqlsrv_query
  • odbc_exec

说明

mixed mssql_query ( string $query [, resource $link_identifier [, int $batch_size = 0 ]] )

mssql_query sends a query to the currently active database on the server that's associated with the specified link identifier.

参数

query
An SQL query.

link_identifier
A MS SQL link identifier, returned by mssql_connect or mssql_pconnect.

If the link identifier isn't specified, the last opened link is assumed. If no link is open, the function tries to establish a link as if mssql_connect was called, and use it.

batch_size
The number of records to batch in the buffer.

返回值

Returns a MS SQL result resource on success, TRUE if no rows were returned, or FALSE on error.

范例

示例 #1 mssql_query example

<?php
// Connect to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');

if (!$link || !mssql_select_db('php', $link)) {
    die('Unable to connect or select database!');
}

// Do a simple query, select the version of 
// MSSQL and print it.
$version = mssql_query('SELECT @@VERSION');
$row = mssql_fetch_array($version);

echo $row[0];

// Clean up
mssql_free_result($version);
?>

注释

Note:

If the query returns multiple results then it is necessary to fetch all results by mssql_next_result or free the results by mssql_free_result before executing next query.

参见

  • mssql_select_db
  • mssql_connect

mssql_result

Get result data

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • odbc_result

说明

string mssql_result ( resource $result , int $row , mixed $field )

mssql_result returns the contents of one cell from a MS SQL result set.

参数

result
The result resource that is being evaluated. This result comes from a call to mssql_query.

row
The row number.

field
Can be the field's offset, the field's name or the field's table dot field's name (tablename.fieldname). If the column name has been aliased ('select foo as bar from...'), it uses the alias instead of the column name.

Note:

Specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.

返回值

Returns the contents of the specified cell.

范例

示例 #1 mssql_result example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username] FROM [php].[dbo].[userlist]');

// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found';
} else {
    for ($i = 0; $i < mssql_num_rows($query); ++$i) {
        echo mssql_result($query, $i, 'username'), PHP_EOL;
    }
}

// Free the query result
mssql_free_result($query);
?>

以上例程的输出类似于:

Kalle
Felipe
Emil
Ross

示例 #2 Faster alternative to above example

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username] FROM [php].[dbo].[userlist]');

// Check if there were any records
if (!mssql_num_rows($query)) {
    echo 'No records found';
} else {
    while ($row = mssql_fetch_array($query)) {
        echo $row['username'], PHP_EOL;
    }
}

// Free the query result
mssql_free_result($query);
?>

注释

Note:

When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mssql_result.

参见

Recommended high-performance alternatives:

  • mssql_fetch_row
  • mssql_fetch_array
  • mssql_fetch_assoc
  • mssql_fetch_object

mssql_rows_affected

Returns the number of records affected by the query

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

  • PDOStatement::rowCount
  • sqlsrv_rows_affected

说明

int mssql_rows_affected ( resource $link_identifier )

Returns the number of records affected by the last write query.

参数

link_identifier
A MS SQL link identifier, returned by mssql_connect or mssql_pconnect.

返回值

Returns the number of records affected by last operation.

范例

示例 #1 mssql_rows_affected example

<?php
// Delete all rows in a table
mssql_query('TRUNCATE TABLE [php].[dbo].[persons]');

echo 'Deleted ' . mssql_rows_affected($link) . ' row(s)';
?>

mssql_select_db

Select MS SQL database

Warning

This function was REMOVED in PHP 7.0.0.

Alternatives to this function include:

说明

bool mssql_select_db ( string $database_name [, resource $link_identifier ] )

mssql_select_db sets the current active database on the server that's associated with the specified link identifier.

Every subsequent call to mssql_query will be made on the active database.

参数

database_name
The database name.

To escape the name of a database that contains spaces, hyphens ("-"), or any other exceptional characters, the database name must be enclosed in brackets, as is shown in the example, below. This technique must also be applied when selecting a database name that is also a reserved word (such as primary).

link_identifier
A MS SQL link identifier, returned by mssql_connect or mssql_pconnect.

If no link identifier is specified, the last opened link is assumed. If no link is open, the function will try to establish a link as if mssql_connect was called, and use it.

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE

范例

示例 #1 mssql_select_db example

<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');

// Select the database 'php'
mssql_select_db('php', $link);
?>

示例 #2 Escaping the database name with square brackets

<?php
// Create a link to MSSQL
$link = mssql_connect('KALLESPC\SQLEXPRESS', 'sa', 'phpfi');

// Select the database 'my.database-name'
mssql_select_db('[my.database-name]', $link);
?>

参见

  • mssql_connect
  • mssql_pconnect
  • mssql_query

目录