Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect php to Microsoft MSSQL

2025-03-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to connect php to Microsoft MSSQL. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

In the study of ezSQL, I saw some functions provided by php, such as mssql_connect (), to connect to MSSQL. I thought that php, an open source programming language popular in the world, should be easy to connect to Microsoft's data, but it was only when it was really implemented that it was found to be a lot of difficulties.

At first, I downloaded the php version 5.93. after a long time of downloading and adding environment variables and so on, the function phpinfo () finally ran successfully in the browser. Then when I was looking for php_mssql.dll all over the world, I found that mssql was no longer the original support for php in version 5.3 and above.

Finally found Microsoft Microsoft Drivers 3.0for PHP for SQL Server, thinking that Microsoft should do what can, but helplessly found that SQLSRV30.EXE can not run: "SQLSRV30.EXE is not an effective win32 program."

After searching the Internet for a long time, I summed up some possible ways to do this, but before you do that, you need:

Configure MICROSOFT SQL SERVER

1. Download and install sql server. Now there are more versions, ranging from 2000 to 2008. Find one and download it yourself.

2. Open the tcp/ip connection mode 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 = 20971520mssql.textsize = 20971520

After you have done this, you can connect to the database in three ways:

Use the method included with php to connect to MSSQL (not applicable in version 5.3 and later)

Make sure there is php_mssql.dll under the php ext extension library folder, and then in the configuration in PHP.ini, set the

; extension=php_mssql.dll

Remove the front ";".

Then you can test the connection:

/ / Connect to MSSQL$conn=mssql_connect ("instance name or server IP", "username", "password"); / / Test connection if ($conn) {echo "connected successfully";}

Microsoft Drivers for SQL Server for PHP

In July 2008, Microsoft released a new driver to connect to SQL Server for php, which improves some of the shortcomings of the connection MSSQL function that comes with php, and is developed in the form of a php extension, through which you can easily read and write Microsoft databases with php.

If your server is using IIS, be sure to download it here:

Http://php.iis.net/

Because the link from the above is actually Microsoft's integrated web development platform, which only provides online installation, but it is very convenient to integrate PDO plug-ins and php, as well as some other Microsoft development functions, but if you don't need it, you don't have to install it. Those are in visual studio.

But if you are using Apache, you can download the plug-in directly here. It is actually an unzipped file and several DLL files are extracted. 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 appears that SQLSRV30.EXE is not a valid win32 program, it may be that some libraries are missing, it may be vc10, or it may not be run with administrator permissions.

Extension_dir = "C:\ PHP\ ext"

3) reference the corresponding dynamic link library file in the php.ini configuration file

Extension=php_sqlsrv_52_ts_vc6.dll

Extension=php_pdo_sqlsrv_52_ts_vc6.dll

Extension=php_pdo.dll

52 and 53 of them indicate that they are 5.2.x and 5.3.x versions of php. Choose the ones that match your php version.

The choice of vc6 or vc9 mainly depends on what web server software you use. If you are using IIS, choose vc9. If it is Apache, choose vc6.

As for ts and nts, it depends on whether you install a thread-safe version of php or a non-thread-safe version. Ts is thread-safe and nts is non-thread-safe.

4) restart Apache

5) connect to the database

Test the connection code:

Using FreeTDS under windows

What is FreeTDS? FreeTDS is actually an open source (or free) C library that can access Microsoft's SQL database under Linux. Can be used in Sybase's db-lib or ct-lib library, which also includes an ODBC library. Allow many applications to connect to Sybase or Microsoft's SQL server. FreeTDS is released as the reality of the source code, which is why it can be compiled and installed on almost any system.

If your server is a Windows system, you should use php_dblib.dll. (more information on Using FreeTDS for Unix.)

Usually we can find these DLL files-Frank Kromann's site on this site, but basically many of them are out of date and cause a lot of problems, so we recommend using PHP 5.2.x under windows and take a look at the following suggestions:

1. Follow the table below to download php_dblib.dll and save it to the / PHP/ext folder.

PHP versionThread SafeFreeTDS versionDownload URLPHP 5.2.x (vc6) Yes0.82 + 20090302 patchesDownloadPHP No0.82 + 20090302 patchesDownloadPHP 5.3.x (vc9) Yes0.82 + 20090904 patchesDownloadPHP No0.82 + 20090904 patchesDownloadPHP 5.4.x (vc9) Yes0.82 + 20110906 patchesDownload! FTP download No0.82 + 20110906 patchesDownload! FTP Download!

2. FreeTDS needs to install .NET Framework v1.1, which you can download from Microsoft's website. Or you can go to Frank's site to download the DLL file you need and save it to your / PHP root directory.

3. Add: in the php configuration file / PHP/php.ini:

Extension=php_dblib.dll

4. When the php engine starts the FreeTDS module, it needs to pass some information so that FreeTDS can connect to its default database. Therefore, it needs to define the basic information of the database connection in freetds.conf, and the file in its root directory can be modified according to your situation:

[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. Create a config.php document to define database connection parameters:

$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. Restart your website. If you are still not connected to your database, change display_startup_errors to "On" in the / PHP/php.ini file, and then change the error report to "Off" after you have solved these problems.

7. Test your website and create a test.php file. The code is as follows. Visit http://localhost/test.php to test.

This is the end of the article on "how to connect php to Microsoft MSSQL". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report