When I was studying ezSQL, I saw mssql_connect() and other functions provided by PHP to connect to MSSQL. I thought that PHP, an open source and popular programming language around the world, should be easy to connect to Microsoft's data, but in reality When executing, I found many difficulties
When I was studying ezSQL, I saw mssql_connect() and other functions provided by PHP to connect to MSSQL. I thought that PHP, an open source and popular programming language around the world, would be very useful for connecting to Microsoft. Data should be a no-brainer, but when it comes to actual implementation, we find that there are many difficulties.
The PHP version I downloaded at the beginning was 5.93. After downloading and adding environment variables for a long time, the phpinfo() function finally successfully ran in the browser. Then when I searched for php_mssql.dll all over the world, I discovered that mssql is no longer natively supported in PHP versions 5.3 and above.
Finally found Microsoft Drivers 3.0 for PHP for SQL Server. I thought that Microsoft’s stuff should be able to do it, but I was helpless to find that SQLSRV30.EXE could not run: "SQLSRV30.EXE is not a valid win32 program. ".
After searching the Internet for a long time, I summarized the following feasible methods, but before that you need to:
Configure MICROSOFT SQL SERVER
1 , download and install sql server. There are many versions now, ranging from 2000 to 2008. Find one and download it yourself.
2. Open the tcp/ip connection method so that the database can be accessed remotely. SQL Server Configuration Manager -> Network Configuration -> Protocols -> TCP/IP enabled
3. Open the data management interface and add users and databases.
4. Install php and configure IIS service.
5. Open the php.ini file in the folder where php is located, and add:
mssql.textlimit = 20971520 mssql.textsize = 20971520
After doing this, you can follow There are three methods to connect to the database:
Use the method that comes with php to connect to MSSQL (not applicable to 5.3 and later versions)
Make sure there is php_mssql.dll in the php ext extension library folder. Then in the configuration in PHP.ini, remove the ";" in front of
;extension=php_mssql.dll
.
Then you can test the connection:
//连接MSSQL
$conn=mssql_connect("实例名或者服务器IP","用户名","密码");
//测试连接
if($conn)
{
echo "连接成功";
}
Microsoft Drivers for SQL Server for PHP
In July 2008, Microsoft released a new driver for php to connect to SQL Server. It improved some shortcomings of php's own MSSQL function and was developed in the form of a php extension plug-in. Through it, you can use php Easily read and write Microsoft databases.
If your server is using IIS, then you must download it from here:
http://php.iis.net/
Because from the above link It is actually a network development platform integrated by Microsoft. It only provides online installation, but it easily integrates PDO plug-ins and PHP. Of course, there are also some other development functions of Microsoft. But if you don’t need it, you don’t need to install it. Those are in visual studio.
But if you are using Apache, you can go here to download this plug-in directly. It is actually an unzipped file, and several DLL files are unzipped. The specific operations are as follows:
1 ) Download the driver package: http://www.microsoft.com/en-us/download/details.aspx?id=20098.
2) Extract the DLL file to the PHP extension_dir directory, if it says SQLSRV30 .EXE is not a valid win32 program. It may be missing some libraries, it may be vc10, or it may not be run with administrator privileges.
extension_dir = “C:\PHP\ext”

3) Reference the corresponding dynamics in the php.ini configuration file Link library file
extension=php_sqlsrv_52_ts_vc6.dll
extension=php_pdo_sqlsrv_52_ts_vc6.dll
extension=php_pdo.dll
52 and 53 represent PHP’s 5.2.x and 5.3 .x version, choose the one that matches your PHP version;
Choosing vc6 or vc9 mainly depends on what web server software you are using. If you are using IIS, choose vc9, if it is Apache, choose vc6 .
As for ts and nts, it depends on whether the PHP version you installed is thread-safe or non-thread-safe. ts is thread-safe and nts is non-thread-safe.
4) Restart Apache
5) Connect to the database
Test connection code:
<?php
//本地测试的服务名
"(local)";
//使用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));
}
?>
at Using FreeTDS under windows
What is FreeTDS? FreeTDS is actually an open source (or can be said to be free) C library, which can access and operate Microsoft's SQL database under the Linux system. It can be used in Sybase's db-lib or ct-lib library, which also contains an ODBC library. Allows many applications to connect to Sybase or Microsoft SQL servers. FreeTDS is released as source code, and because of this, it can be compiled and installed on almost any system.
If your server is a Windows system, then you should use php_dblib.dll. (more information on Using FreeTDS for Unix.)
通常我们可以在这个网站上找到这些DLL文件- Frank Kromann's site, 但是它基本上很多都已经过时了,并且会引发很多的问题,因此我们推荐windows下使用PHP 5.2.x版本,并且看看下面的建议 :
1. 按照下面的表格来下载 php_dblib.dll 并且将其保存到 /PHP/ext 文件夹下面.
| PHP version | Thread Safe | FreeTDS version | Download URL |
|---|---|---|---|
| PHP 5.2.x (vc6) | Yes | 0.82 + 20090302 patches | Download! |
| No | 0.82 + 20090302 patches | Download! | |
| PHP 5.3.x (vc9) | Yes | 0.82 + 20090904 patches | Download! |
| No | 0.82 + 20090904 patches | Download! | |
| PHP 5.4.x (vc9) | Yes | 0.82 + 20110906 patches | Download! FTP Download! |
| No | 0.82 + 20110906 patches | Download! FTP Download! |
2、 FreeTDS 需要安装 .NET Framework v1.1 ,你可以到微软的网站去下载。或者你去Frank's site 下载需要的DLL文件,并保存到你的/PHP根目录下面。
3、在php配置文件 /PHP/php.ini 中添加:
extension=php_dblib.dll
4、当php引擎启动FreeTDS模块的时候需要传递一些信息,使得FreeTDS能够连接到它的默认的数据库。因此它的需要在freetds.conf中定义数据库连接的基本信息,该文件在其根目录下,可以按照你的情况来进行修改:
[global]
host = xxx.xxx.xxx.xxx (host name or ip of the MSSQL server)
port = 1433
client charset = UTF-8
tds version = 8.0
text size = 20971520
5、创建config.php文档来定义数据库连接参数:
$CFG->dbtype = 'mssql'; // Required $CFG->dbhost = 'localhost'; // assuming MS SQL is on the same server, otherwise use an IP $CFG->dbname = 'moodle'; // or whatever you called the database you created $CFG->dbuser = 'yourusername'; // I usually use the 'sa' account (dbowner perms are enough) $CFG->dbpass = 'yourpassword'; $CFG->dbpersist = false; $CFG->prefix = 'mdl_'; //Prefix, you can change it, but NEVER leave it blank.
6、 重启你的网站,如果还是没有连接到你的数据库的话,在 /PHP/php.ini文件中将display_startup_errors改为"On",当你解决了这些问题之后再将错误报告改为“Off”;
7、测试你的网站,建立test.php文件,代码如下,访问http://localhost/test.php进行测试
<?php
$link = mssql_connect('localhost', 'db_user', 'db_password');
if(!$link) {
echo'Could not connect';
die('Could not connect: ' . mssql_error());
}
echo'Successful connection';
mssql_close($link);
?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
The above is the detailed content of Guide to connecting Microsoft MSSQL (sql server) with php. For more information, please follow other related articles on the PHP Chinese website!
PHP: An Introduction to the Server-Side Scripting LanguageApr 16, 2025 am 12:18 AMPHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.
PHP and the Web: Exploring its Long-Term ImpactApr 16, 2025 am 12:17 AMPHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.
Why Use PHP? Advantages and Benefits ExplainedApr 16, 2025 am 12:16 AMThe core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.
Debunking the Myths: Is PHP Really a Dead Language?Apr 16, 2025 am 12:15 AMPHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.
The PHP vs. Python Debate: Which is Better?Apr 16, 2025 am 12:03 AMPHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.
PHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AMPHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.
PHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AMPHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.
How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AMUsing preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use






