search
HomeBackend DevelopmentPHP ProblemSolution to the problem that the php system does not support mssql

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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool