PHP: MS SQL サーバー関数 - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<mime_content_typemssql_bind>
view the version of this page
Last updated: Tue, 21 Dec 2004

LXV. MS SQL サーバー関数

導入

以下の関数によりMS SQLサーバデータベースにアクセス可能となります。

注意: MSSQL拡張モジュールは、Win32システムでのみ利用可能です。他のプラッ トフォームからMSSQLデータベースに接続するには、 Sybase拡張モジュール を使用することが可能です。

要件

この拡張モジュールは、PHPがインストールされているシステムにMS SQL クライアントツールのインストールを要します。クライアントツールは、 MS SQL Server CDまたは\winnt\system32から ntwdblib.dllをPHPシステム の\winnt\system32にコピーすることによりインストール可能です。 ntwdblib.dllのコピーではアクセスのみが可能で す。クライアントの設定は、全てのツールのインストールが必要です。

インストール手順

MSSQL拡張モジュールは、extension=php_mssql.dllをphp.iniに追加する ことにより、利用可能となります。

実行用の設定

これらの関数の動作は、php.iniの設定により変化します。

表 1. MS SQLサーバ設定オプション

名前デフォルト変更の可否
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.compatability_mode"0"PHP_INI_ALL
mssql.connect_timeout"5"PHP_INI_ALL
mssql.timeout"60"PHP_INI_ALL
mssql.textsize"-1"PHP_INI_ALL
mssql.textlimit"-1"PHP_INI_ALL
mssql.batchsize"0"PHP_INI_ALL
mssql.datetimeconvert"1"PHP_INI_ALL
mssql.secure_connection"0"PHP_INI_SYSTEM
mssql.max_procs"25"PHP_INI_ALL
PHP_INI_* 定数の詳細と定義については、 ini_set()を参照して下さい。

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

MSSQL_ASSOC (integer)

MSSQL_NUM (integer)

MSSQL_BOTH (integer)

SQLTEXT (integer)

SQLVARCHAR (integer)

SQLCHAR (integer)

SQLINT1 (integer)

SQLINT2 (integer)

SQLINT4 (integer)

SQLBIT (integer)

SQLFLT8 (integer)

目次
mssql_bind --  ストアドプロシージャまたはリモートストアドプロシージャへパラメー タを追加する
mssql_close -- MS SQL サーバーへの接続を閉じる
mssql_connect -- MS SQL サーバー接続をオープンする
mssql_data_seek -- 内部行ポインタを移動する
mssql_execute --  MS SQLサーバデータベースでストアドプロシージャを実行する
mssql_fetch_array -- 配列として行を取得する
mssql_fetch_assoc --  result_idで指定した結果集合でカレントの行を連想配列として返す
mssql_fetch_batch --  レコードの次のバッチを返す
mssql_fetch_field -- フィールド情報を取得する
mssql_fetch_object -- オブジェクトとして行を取得する
mssql_fetch_row -- 配列として行を取得する
mssql_field_length -- フィールド長を得る
mssql_field_name -- フィールド名を得る
mssql_field_seek -- フィールドオフセットを設定する
mssql_field_type -- フィールド型を得る
mssql_free_result -- 結果保持用メモリを解放する
mssql_free_statement -- Free statement memory
mssql_get_last_message --  サーバーの直近のメッセージを返す (min_message_severityに依存する?)
mssql_guid_string --  16バイトバイナリGUIDを文字列に変換する
mssql_init --  ストアドプロシージャまたはリモートのストアドプロシージャを初期化 する
mssql_min_error_severity -- エラー判定基準を小さく設定する
mssql_min_message_severity -- メッセージ判定基準を小さく設定する
mssql_next_result -- 次の結果に内部結果ポインタを移動する
mssql_num_fields -- 結果のフィールド数を得る
mssql_num_rows -- 結果の行数を得る
mssql_pconnect -- 持続的 MS SQL 接続をオープンする
mssql_query -- MS SQL クエリーを送る
mssql_result -- 結果データを得る
mssql_rows_affected --  クエリにより取得したレコード数を返す
mssql_select_db -- MS SQL データベースを選択する


add a note add a note User Contributed Notes
MS SQL サーバー関数
marcel at fluidleb dot com
31-Jan-2005 06:24
I've spent the last three days wild guessing why I could not connect to MS SQLServer 2000 and so I hope this might help someone else. Microsoft has purposefully closed the 1433 tcp port and others (maybe only to external ips) of the server. You can easily check if this is the case by looking at the Event Monitor. You should see an error produced by MSSQL SERVER/MSDE that basically says what I said. It suggests downloading and installing a security patch to open up the ports (but doesn't even say which update - remember this is Microsoft).

Do download Service Pack 3a (SP3a). It does indeed fix the problem.
Salman Majid
25-Jan-2005 05:36
Sorry, previous message was incomplete. I used odbc functions to connect to Ms-SQL server. I was on a work station and server was not accessible for unknow reasons. mssql functions did not work there so I had to use odbc.
Salman Majid
25-Jan-2005 05:15
I was having problem in connecting Ms-SQL server with mssql_connect(srvr,uid,pwd). I simply created a DSN to my Database and replaced it with "server_name". i.e.

mssql_connect(dsn,uid,pwd);

it worked well
craig at _NO_SPAM_deconstructor dot net
13-Jan-2005 08:22
Just a quick note pertaining marcos1979ar (10/06/04) definitive installation of FreeTDS above.

When you run the following to test the installation:
===============================================================
/usr/local/freetds/bin/tsql -S <ip of the server> -U <User SQL>
===============================================================

And it results in the following error:
=================================================================
locale is "C"
locale charset is "ANSI_X3.4-1968"
Password:
src/tds/login.c: tds_connect: xx.xx.x.xx:1443: Connection refused
Msg 20009, Level 9, State 0, Server OpenClient, Line 0
Server is unavailable or does not exist.
There was a problem connecting to the server
=================================================================

All I found that needed to be changed was for the configure --prefix from:

=======================================
./configure --prefix=/usr/local/freetds
=======================================
         to
=======================================
./configure --prefix=/usr/local
=======================================

Small but worth noting.
hbinduni at hslprpol dot com
20-Dec-2004 08:15
i found working fast, easy to use and to configure, and yes, it's multiplatform.
marc at NOSPAM dot durdin dot net
19-Nov-2004 04:19
Buried away in the mssql_field_length documentation is an important limitation that it is certainly worth knowing about *BEFORE* you do any database design:

Note to Win32 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.

SQL Server natively supports VARCHAR up to 8000 characters.  Note that TEXT fields have substantially poorer performance (and are much more limited) than VARCHAR so you may want to design your databases accordingly...

You can also work around this limitation with the following:

   -- for example, with MyVarCharField VARCHAR(1000)
   SELECT CAST(MyVarCharField AS TEXT) FROM MyTable
ASWL
28-Oct-2004 12:55
This note is for mssql under a Windows environment.

If you are having problems connecting to MS SQL using the mssql PHP extension then I would suggest that you get a copy of the latest NTWDBLIB.DLL. Copy it to the directory where PHP expects to find it's dll files.

I had an issuse whereby I was unable to connect to a MS SQL Cluster and this resolved my problem.
Dimitri Tarassenko (mitka at mitka.us)
24-Oct-2004 07:37
To enable MS SQL functions in PHP on Fedora Core or RHEL3 without recompiling PHP and replacing the distribution PHP packages, download and install php-mssql and freetds RPMS from

effeese at gmail dot com
15-Oct-2004 01:28
in relation to the "unicode problem" mentioned by andrej at dairyweb dot com dot au I suggest use andrew at power-grid dot com's solution:

if you get a "Unicode data in Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier" error you can use this kind of code (in an MS environment, of course):
<?
$db
= new COM("ADODB.Connection");
$dsn = "DRIVER={SQL Server}; SERVER={SERVER};UID={USER};PWD={PASS}; DATABASE={DB}";
$db->Open($dsn);
$rs = $db->Execute("SELECT * FROM table");

while (!
$rs->EOF)
{
   echo
$rs->Fields['column']->Value."<BR>";
  
$rs->MoveNext();
}
?>

hope it helps.
mworku at yahoo dot com
03-Oct-2004 04:49
If you want to utilize the freetds.conf file db parameters within
mssql_connect() function

<?
   putenv
('FREETDSCONF=/etc/freetds.conf');

    
/*
       mssql_connect('MSSQL_8.0' , 'user' , 'pwd');
       ....
       ....
   */
?>

FreeTDS conf file

 [MSSQL_8.0]
   host = 10.254.254.10
   port = 1433
   tds version = 8.0
08-Aug-2004 10:11
For phpBB if you are having a problem with user cannot connect to database I found that gentiboard's comments were the answer.

You have to change sql secure connection to on in php.ini
marcos1979ar at yahoo dot com dot ar
10-Jun-2004 10:07
Red Hat 9.0  Freetds 0.62.3  Apache 2.0.49  PHP 5.0.ORC3

Instucction, that premit connect a server Linux with a server Windows 2000 Server with SQL Server.

INSTALLING FREETDS
 
1-.  Download freetds -> www.freetds.org
2-.  tar -zxvf freetds-stable-tgz
3-.  cd freetds-0.62.3
4-.  ./configure --prefix=/usr/local/freetds --with-tdsver=8.0 --enable-msdblib --enable-dbmfix --with-gnu-ld

Note:  tdsver=8.0 if you use SQL 2000, tdsver=7.0 if you use SQL 7.0

5-.  make
6-.  make install
7-.  /usr/local/freetds/bin/tsql -S <ip of the server> -U <User SQL>

Note: For default User SQL is    sa    and the it have not password
For example: /usr/local/freetds/bin/tsql -S 198.168.100.2 -U sa

8-.  Add the next text in freetds.conf ( /usr/local/freetds/etc )
[TDS]
host = <ip of the Server with Sql>
port = 1433
tds version = 8.0

Note: If you use SQL 2000 then tds version = 8.0
if you use SQL 7.0 then tds version = 7.0

9-.  Add the next text in the file /etc/ld.so.conf
/usr/local/freetds/lib

INSTALLING  APACHE

1-.  Download apache  www.apache.org
2-.  tar -zxvf httpd-2.0.49.tar.gz
3-.  cd httpd-2.0.49
4-.  ./configure --prefix=/etc/httpd --enable-so
5-.  make
6-.  make install
7-.  Configure the file  -> httpd.conf ( /etc/httpd/conf/httpd.conf )
8-.  Probe the apache: /etc/httpd/bin/apachectl start
/etc/httpd/bin/apachectl stop

INSTALLING  PHP

1-.  Download the PHP in this site (www.php.net)
2-.  tar -zxvf php-5.0-ORC3.tar.gz
3-.  cd php-5.0-ORC3
4-.  ./configure --with-apxs2=/etc/httpd/bin/apxs --enable-versioning --with-mssql=/usr/local/freetds --disable-libxml
5-.  make
6-.  make install
7-.  cp php.ini-DIST /usr/local/lib
8-.  Add the next line in /etc/httpd/conf/httpd.conf
AddType application/x-httpd-php .php

TESTING

<html>
<body>
<?php
$con
= mssql_connect ("<ip of the server SQL>", "sa", "");
mssql_select_db ("<Data Base>", $con);
$sql= "SELECT * FROM <Table>";
$rs= mssql_query ($sql, $con);
echo
"The field number one is: ";
echo
mssql_result ($rs, 0, 0);
mssql_close ($con);
?>
</body>
</html>
Asad Moin
29-May-2004 02:48
I took the following steps to make PHP talk to SQL2000:

- Copy ntwdblib.dll file from your SQL server's  \winnt\system32 folder to the \winnt\system32 folder on the PHP box.

- Copy the php_mssql.dll from the 'dlls/' directory in distribution package to your windows/system (Win9.x) or winnt/system32 (WinNT, Win2000, XP) directory.

- Add or uncomment the following line under Windows Extentions in your php.ini file (\winnt\php.ini): extension=php_mssql.dll

Hope that helps.
andrew at power-grid dot com
26-May-2004 07:11
Ok, this took me a few days to get right but for those of you out there having a hard time with getting PHP to talk to SQL 2000 this is for you!  Personally for me the mssql plugin barely worked with sql 2000 and was VERY slow, here's your solution and also some good code to connect securely and quickly through ADO. I was having the worst time getting ADO to work as well, most sites do the driver part a different way.. for me the code below is the only thing that worked.
<?
$db
= new COM("ADODB.Connection");
$dsn = "DRIVER={SQL Server}; SERVER={SERVER};UID={USER};PWD={PASS}; DATABASE={DB}";
$db->Open($dsn);
$rs = $db->Execute("SELECT * FROM table");

while (!
$rs->EOF)
{
   echo
$rs->Fields['column']->Value."<BR>";
  
$rs->MoveNext();
}
?>
Arthur W Rudd
26-May-2004 06:37
I'm running apache 1.3.26 and php 4.3.6 on a linux server (debian), and connecting to SQL Server 2000 via php4-mssql and Freetds.

I managed to get a connection to the SQL server, but found that when I used the function mssql_query() to pass in a Select statement containing datetime columns, I would get the following message in Internet Explorer - "The page cannot be displayed".  When I removed the datetime column from the query, it worked fine, and returned a resultset.
Looked in the apache error.log file, and found the error "child pid xxxxxx exit signal segmentation fault".

After a lot of searching on the internet I stumbled upon an entry which was missing from my php.ini.  I added the line "mssql.datetimeconvert = Off" to the MSSQL section of pphp.ini, restarted Apache, and the problem went away.  Now I can select dates in SQL queries
jasenko at get2net dot dk
05-May-2004 06:46
If you get a crash trying to get currency from database with smallmoney datatype and big value (in my case it was 15000..), change datatype to money instead. The error is gone.

I'm using php 4.2.3 and MSSQL 7.0 on windows.
andrej at dairyweb dot com dot au
28-Apr-2004 08:49
if you are having problems with freeTDS, or like me you are trying to get access to unicode data from mssql.

try it is cross platform and (reportedly) faster than freeTDS. it is very stable and easy to configure.

i was originally trying to solve an error message of "Unicode data in Unicode-only collation or ntext data cannot be sent to clients using DB-Library (such as ISQL) or ODBC version 3.7 or earlier" and found this solution.

hope it helps someone.
elmer at web-axis dot net
28-Apr-2004 04:52
Here's something that could save you 2-3 days research trying to figure out:

<?php
putenv
('TDSDUMP=/tmp/tds.log');
putenv('SYBASE=/usr/lib/freetds');
putenv('TDSVER=70');
?>

If you have problems getting the mssql functions to work try these varibles. MSSQL/PHP has problems dealing with different tds versions. 7.0 seems to work well with our server version, maybe a different version would work better with yours.
pmfa at versenet dot com
27-Apr-2004 04:26
If you're going to use freeTDS just to access mssql databases, supply the "--enable-msdblib" switch to the freeTDS configure script when building the library. This will fix some obscure problems, like the month in a date not being displayed correctly, more information in
gentiboard2002 at yahoo dot de
19-Apr-2004 01:55
there's no documentation of the php.ini option mssql.secure_connection.

so i have here a MS SQL 2000 Server with Windows Authentication and a Windows 2003 Server with IIS 6. So it is clear that i cannot use the usual handling of a mssql_connect.
I turned the mssql.secure_connection on.
I let the Windows IIS to run under an Service Account(normal user account without any special rights) in a Windows Domain, so i do have a authenticated connection to the SQL Server...

Everything works fine without any rewrite of the code...
19-Mar-2004 08:42
Note: DBLib is obsolete and only supports SQL Server 4.x, 6.5, 7.0 and 8.x.
jholland at cs dot selu dot edu
10-Dec-2003 09:37
To enable mssql in php 4.3.3 and above on linux/unix boxes, you must put this line in php.ini

extension=mssql.so

<mime_content_typemssql_bind>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page