Home  >  Article  >  Backend Development  >  Solution to the problem that the php system does not support mssql

Solution to the problem that the php system does not support mssql

藏色散人
藏色散人Original
2021-09-11 10:16:341889browse

Solution for php not supporting mssql: 1. Download "SQLSRV20.EXE"; 2. Extract the file to the php extension folder ext; 3. Modify the php.ini file; 4. Save and restart apache That’s it.

Solution to the problem that the php system does not support mssql

The operating environment of this article: windows7 system, php5.3 version, DELL G3 computer

php system does not support mssql problem solving Method, solution to the problem that php5.3 cannot connect to the mssql database

The example in this article describes the solution to the problem that php5.3 cannot connect to the mssql database.

The analysis is as follows:

Since php5.3, the system does not support the function mssql_connect. In the past, I also said that it can be implemented using the com interface. Now I will introduce the solution to the problem that php5.3 cannot Another way to connect to mssql database.

Under Windows system, versions above PHP5.3 no longer support mssql extension.

First http://msdn.microsoft.com/en-us/ sqlserver/ff657782.aspx Click get it to download SQLSRV20.EXE.

Extract the file to the php extension folder ext, open php.ini and add at the end:

The code is as follows:

[PHP_PDO_SQLSRV] 
extension=php_pdo_sqlsrv_53_nts_vc6.dll 
[PHP_SQLSRV] 
extension=php_sqlsrv_53_nts_vc6.dll

Restart apache after saving, attached A simple PHP connection example, the code is as follows:

The code is as follows:

<?php 
$serverName = "(127.0.0.1)"; 
$connectionInfo = array( "UID"=>"root", 
"PWD"=>"root2010", 
"Database"=>"master"); 
 
$conn = sqlsrv_connect( $serverName, $connectionInfo); 
if( $conn ) 
{ 
echo "Connection established.n"; 
} 
else 
{ 
echo "Connection could not be established.n"; 
die( print_r( sqlsrv_errors(), true)); 
} 
?>

I am using the wamp5.1 integrated installation package, tested on Windows Server 2008, PHP5.4 The above version test was not successful.

If you use this extension to connect to SQL Server 2005 or above (such as SQL Server 2008), you also need to install SQL Server Native Client on the machine first: http://download .microsoft.com/download/0/E/6/0E67502A-22B4-4C47-92D3-0D223F117190/sqlncli.msi

This extension adds a series of functions starting with sqlsrv_ to PHP. The function reference is as follows :

The code is as follows:

sqlsrv_begin_transaction 
sqlsrv_cancel 
sqlsrv_client_info 
sqlsrv_close 
sqlsrv_commit 
sqlsrv_configure 
sqlsrv_connect 
sqlsrv_errors 
sqlsrv_execute 
sqlsrv_fetch 
sqlsrv_fetch_array 
sqlsrv_fetch_object 
sqlsrv_fetch_metadata 
sqlsrv_free_stmt 
sqlsrv_get_config 
sqlsrv_get_field 
sqlsrv_has_rows 
sqlsrv_next_result 
sqlsrv_num_fields 
sqlsrv_num_rows 
sqlsrv_prepare 
sqlsrv_query 
sqlsrv_rollback 
sqlsrv_rows_affected 
sqlsrv_send_stream_data 
sqlsrv_server_info

More detailed instructions can be found in the SQLServerDriverForPHP.chm help file in the self-extracting file just now. Open it and click the API Reference node.

Look at another odb connection method, the code is as follows:

The code is as follows:

$dbhost = &#39;&#39;; 
$dbuser = &#39;&#39;; //你的mssql用户名 
$dbpass = &#39;&#39;; //你的mssql密码 
$dbname = &#39;&#39;; //你的mssql库名 
$connect=odbc_connect("Driver={SQL Server};Server=$dbhost;Database=$dbname","$dbuser","$dbpass"); 
$sql="select * from content"; 
$exec=odbc_exec($connect,$sql); 
while($row = (odbc_fetch_array($exec))) 
{ 
$row[&#39;id&#39;]   //?取字段值 
… 
}

Recommended learning: "PHP Video Tutorial"

Related introduction:

1. The steps for php5.3 to successfully connect to MSSQL through PDO are briefly summarized as follows:

1. Download microsoft drivers for php for sql server (currently 2.0 3.0 Versions are divided into ts and nts versions respectively. You can confirm that it is ts through Thread Safety:enable in phpinfo(). For details, please refer to the manual that comes with the driver)

2. Copy the ext file of the driver to the php folder After folder, modify the php.ini extension extension=php_pdo_sqlsrv_53_ts_vc6.dll

3. Download Microsoft SQL Server 2008 R2 Native Client installation

Test code:

<?php
  try {
     $hostname = "192.168.1.100"; 
     $dbname = "Northwind";
     $username = "sa"; 
     $pwd = "pwd100"; 
     $dsn="sqlsrv:Server=$hostname;database=$dbname";
     
    $conn = new PDO ($dsn,$username,$pwd);
    $conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    echo "mssql database connnection sucessed!";
   } catch (PDOException $e) {
     echo "Failed to get DB handle: " . $e->getMessage() . "\n";
     exit;
   }
?>

2. Windows system Next, versions above PHP5.3 no longer support the mssql extension, so if you need to communicate with the sql server, you need to download The SQL provided by Microsoft at http://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx Server Driver for PHP. This is a self-extracting file. After decompression, you will get the following files:

php_sqlsrv_52_nts_vc6.dll
php_sqlsrv_52_ts_vc6.dll
php_sqlsrv_53_nts_vc6.dll
php_sqlsrv_53_nts_vc9.dll
php_sqlsrv_53_ts_vc6.dll
php_sqlsrv_53_ts_vc9.dll
php_sqlsrv_license.rtf
SQLServerDriverForPHP.chm
SQLServerDriverForPHP_Readme.htm

Among them, 52 and 53 represent the versions of PHP 5.2.X and 5.3.X; nts represents non-linear and secure, and ts represents Thread safety; vc6 means using Apache as the Web Server, vc9 means using IIS as the Web Server.

According to your configuration, copy the corresponding DLL file to the ext folder of the PHP installation directory, then open php.ini and add the following statements to open the php_sqlsrv and php_pdo_sqlsrv extensions:

— ——————————————————–

[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_ts_vc6.dll
[PHP_SQLSRV]
extension=php_sqlsrv_53_ts_vc6.dll

————————————————

Here 53 means php5.3. If yours is version 5.2, change it to 52. If your PHP version is thread-safe, then there should be a php5ts.dll in your PHP installation directory, which is the same as the two lines of statements here. Correspondingly, if it is php5nts.dll, then the above statement should be:

————————————————————-

[PHP_PDO_SQLSRV]
extension=php_pdo_sqlsrv_53_nts_vc6.dll
[PHP_SQLSRV]
extension=php_sqlsrv_53_nts_vc6.dll

————————————————-

There are dll files for each version in the compressed package, you can check it carefully.

After turning on the extension, restart apache so that you can connect to sqlserver, but there is one more thing to note. If you have not installed Microsoft SQL Server 2008 R2 Native Client, you must go to http://msdn.microsoft.com Download and install /en-us/library/cc296170(SQL.90).aspx, because this extension package from Microsoft requires this support.

After everything is in place, you can write PHP code. If you downloaded The SQL Server Driver for PHP, there is a help document in the unzipped folder. You can easily find examples. Here the webmaster will introduce a simple example:

<?php
//本地测试的服务名
$serverName = “(127.0.0.1)”;
//使用sql server身份验证,参数使用数组的形式,一次是用户名,密码,数据库名
//如果你使用的是windows身份验证,那么可以去掉用户名和密码
$connectionInfo = array( “UID”=>”root”,
“PWD”=>”root2010″,
“Database”=>”master”);
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn )
{
echo “Connection established.\n”;
}
else
{
echo “Connection could not be established.\n”;
die( print_r( sqlsrv_errors(), true));
}
?>

If the connection is unsuccessful, restart the sql server and try again.

The above is the detailed content of Solution to the problem that the php system does not support mssql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn