PHP connection Microsoft MSSQL (SQL Server) Full introduction

Source: Internet
Author: User
Tags install php odbc mssql mssql server sybase vc9 win32

Http://www.jb51.net/article/98364.htmphp Connect Microsoft MSSQL (SQL Server) Full introduction

Noisy font: [Increase decrease] Type: Reprint time: 2016-11-27 I want to comment

In the study of Ezsql, I saw the Mssql_connect () and other PHP to provide the connection MSSQL function, Ben thought that the open source of PHP in the world's popular programming language to connect Microsoft's data should be a cinch, but to the real implementation of the time, only to find the difficulties

In the study of ezsql time to see the Mssql_connect () and other PHP to provide the connection MSSQL function, Ben thought that the open source of PHP in the world's popular programming language to connect Microsoft's data should be a cinch, but to the real implementation of the time, only to find a lot of difficulties.

At first I downloaded the PHP version is 5.93, download to add environment variables and so on after a long day, phpinfo () This function finally successfully run in the browser. Then when I was looking for php_mssql.dll in the all over the years, I found that in PHP 5.3 and above, it was not the original eco-support MSSQL.

Finally found Microsoft Microsoft Drivers 3.0 for PHP for SQL Server, thought Microsoft's things should do can, but helpless found SQLSRV30.EXE no way to run: "SQLSRV30. EXE is not a valid Win32 program. "

Search the internet for half a day, summed up the following some feasible methods, but before you need to:

Configure Microsoft SQL SERVER

1. Download and install SQL Server. Now this version is more than 2000 to 2008, find yourself a download it.

2, open TCP/IP connection mode, so that the database can be accessed remotely. TCP/IP enabled, protocols, Network configuration, SQL Server configuration Manager

3. Open the Data management interface to add users and databases.

4. Install PHP and configure IIS services.

5. Open the php.ini file in the folder where PHP is located, and add:

?
12 mssql.textlimit = 20971520mssql.textsize = 20971520

Once you've done this, you can connect to the database in the following three ways:

Connect MSSQL using PHP's own method (not applicable for 5.3 and later versions)

Make sure that the PHP Ext Extension Library folder has Php_mssql.dll, and then in the configuration in PHP.ini,

; Extension=php_mssql.dll

The preceding ";" Remove.

Then you can test the connection:

?
12345678 //连接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, which improved PHP's own connection to the MSSQL function, and was developed in the form of PHP extensions, through which you can easily read and write Microsoft's database with PHP.

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

http://php.iis.net/

Because from the above link in fact is Microsoft Integrated Network development platform, only provide online installation, but it is easy to integrate the PDO plugin and PHP, of course, there are some other Microsoft development features, but if you do not need, you can not install, those are in Visual Studio.

But if you are using Apache, you can go here to download the plugin directly, it is actually an extract files, extracted several DLL files, the specific operation is as follows:

1) Download the driver package: http://www.microsoft.com/en-us/download/details.aspx?id=20098.

2) to extract the DLL files to the PHP Extension_dir directory, if it appears that SQLSRV30.EXE is not a valid Win32 program, may be missing some libraries, may be VC10, there may be no administrator permissions to run.

Extension_dir = "C:\PHP\ext"

3) reference the corresponding dynamic link library file within 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, 53 means PHP 5.2.x and 5.3.x version, choose to match your PHP version;
Choose VC6 or vc9 the main look at what you use the Web server software, if you are using IIS then choose Vc9, if it is Apache chose VC6.
As for TS and NTS, it depends on whether the version of PHP you are installing is thread-safe or non-threaded, and TS is thread safe and NTS is non-thread-safe.

4) Restart Apache
5) Connect to the database

To test the connection code:

?
123456789101112131415161718192021 <?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));} ?>

Using FreeTDS under Windows

What is FreeTDS? FreeTDS is actually an open source (or can be said to be free) C library, it can be implemented under the Linux system Access operation of Microsoft's SQL database. Can be used in Sybase's Db-lib or Ct-lib library, which also contains an ODBC library. Many applications are allowed to connect to Sybase or Microsoft's SQL Server. FreeTDS is released as a source of code, and because of this, it can be compiled and installed in almost any system.

If your server is a Windows system, then you should use Php_dblib.dll. (More information with Using FreeTDS for Unix.)

Usually we can find these DLL files on this website-Frank Kromann's site, but it's basically a lot of outdated and will cause a lot of problems, so we recommend using PHP 5.2.x version under Windows, and take a look at the following suggestions:

1. Follow the form below to download the Php_dblib.dll and save it to the/php/ext folder.

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 need to install the. NET Framework v1.1, you can go to Microsoft's website to download. Or you go to Frank's site to download the required DLL file and save it to your/php root directory.

3. Add in PHP config file/php/php.ini:

Extension=php_dblib.dll

4. When the PHP engine launches the FreeTDS module, it needs to pass some information so that FreeTDS can connect to its default database. So it needs to define the basic information of the database connection in freetds.conf, the file under 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 the database connection parameters:

?
1234567 $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 site, if you are still not connected to your database, in the/php/php.ini file will be display_startup_errors to "on", when you solve these problems and then change the error report to "OFF";
7, test your website, establish test.php file, code as follows, visit http://localhost/test.php to test

?
123456789 <?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);?>

All right, Windows. Using FreeTDS online Information A lot of it is no longer said, so far, this article log end.

If you have questions about this article, please click into the Scripting House knowledge community to ask questions.

Articles you may be interested in:
    • Simple implementation method of PHP extended remote connection MSSQL based on MSSQL
    • PHP-based PDO connection MSSQL sample Demo
    • PHP Connection MSSQL Method Summary
    • php5.3 Unable to connect MSSQL database solution
    • PHP connects MSSQL Server DB instance with PDO
    • Example of the MSSQL database connection class in PHP
    • PHP Connection mssql2008/2005 Database (SQLSRV) configuration instance
    • Several ways to connect MSSQL database with PHP
    • PHP connection mssql Some of the relevant experience and considerations
    • About PHP Connections mssql:pdo ODBC SQL Server
    • Code to connect mssql2005 under PHP
    • PHP connection MSSQL Database beginner PHP notes
    • PHP ADODB connection MSSQL solve garbled problem

PHP connection Microsoft MSSQL (SQL Server) Full introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.