PHP connection Microsoft MSSQL (SQL Server) Full introduction

Source: Internet
Author: User
Tags mssql mssql server sybase vc9
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:

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:

Connect Mssql$conn=mssql_connect ("instance name or server IP", "username", "password");//test connection if ($conn) {echo "Connection succeeded";}

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.dllextension=php_pdo_sqlsrv_52_ts_vc6.dllextension=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:

<?php//Local Test Service name "(local)";//Use SQL Server Authentication, parameters using the form of an array, once is the user name, password, database name//If you are using Windows authentication, then you can remove the user name and password $ ConnectionInfo = Array ("UID" = "root",    "PWD" = "root2010",    "Database" = "master"); $conn = Sqlsrv_ Connect ($serverName, $connectionInfo), if ($conn) {echo "Connection established.\n";} else{echo "Connection could not being 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 versionthread Safefreetds versiondownload urlphp 5.2.x (VC6) Yes0.82 + 20090302 patchesdownload! No0.82 + 20090302 patchesdownload! PHP 5.3.x (VC9) Yes0.82 + 20090904 patchesdownload! No0.82 + 20090904 patchesdownload! PHP 5.4.x (VC9) Yes0.82 + 20110906 patchesdownload! FTP download! No0.82 + 20110906 patchesdownload! 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 = 1433client CharSet = Utf-8tds Version = 8.0text Size = 20971520

5. Create a config.php document to define the database connection parameters:

$CFG->dbtype = ' mssql '; Required$cfg->dbhost = ' localhost '; Assuming MS SQL is on the same server, otherwise with an ip$cfg->dbname = ' Moodle ';  Or whatever you called the database you created$cfg->dbuser = ' yourusername '; I usually the ' sa ' account (dbowner perms is enough) $CFG->dbpass = ' YourPassword '; $CFG->dbpersist = false; $C Fg->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://www.php.cn/to test

<?php$link = Mssql_connect (' localhost ', ' db_user ', ' Db_password '), if (! $link) {echo ' Could not connect ';d ie (' Could Not connect: '. Mssql_error ());} Echo ' successful connection '; Mssql_close ($link);? >

The above is the PHP connection to Microsoft MSSQL (SQL Server) Full strategy content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.