Several methods for connecting php to the mssql database _ PHP Tutorial

Source: Internet
Author: User
Tags odbc connection
Several methods for connecting php to the mssql database. First, remove the semicolon before the extensionphp_pdo_mssql.dll and extensionphp_pdo_odbc.dll in the php. ini file. Restart the service to make it take effect. First, remove the semicolon in the php. ini file; extension = php_pdo_mssql.dll; extension = php_pdo_odbc.dll. which method is used to connect to mssql. Restart the service to make it take effect.

1. establish a connection

1. odbc

First, set odbc on the server where the php program is located. The 32-bit and 64-bit operating systems are different here. You can directly create a 32-bit data source (odbc) from the management tool in the control panel, and run C: \ Windows \ SysWOW64 \ odbcad32.exe in 64-bit mode.

Set from here. Note: The above is only the case where the database server is 32 characters and the data source is set to 32-bit or 64-bit. As long as the two servers have the same data source bits.

The following is the odbc Connection creation code.

The code is as follows:


$ Con = odbc_connect ('odbc name', 'username', 'password ');


2. connect to mssql2000

The code is as follows:


$ Con = mssql_connect ('database address', 'username', 'password ');

3. connect to mssql2008

The code is as follows:


$ ConnectionInfo = array ("UID" => username, "PWD" => password, "Database" => "Database name ");
$ Con = sqlsrv_connect (database address, $ connectionInfo );

II. enter the query code

This is the same. you can write data directly or copy it from mssql. Simply put, an SQL statement is assigned to a variable.

Code similar to the following

The code is as follows:


$ Query = "SELECT top 12 * database name order by id desc ";


3. create a query and retrieve data

1. odbc

The code is as follows:


$ Result = odbc_do ($ con, $ query );
While (odbc_fetch_row ($ result ))
{
$ Variable name = odbc_result ($ result, "Field name ");
}


2. connect to mssql2000

The code is as follows:


$ Result = mssql_query ($ con, $ query );
While ($ row = mssql_fetch_array ($ result ))
{
$ Variable name = $ row ["field name"];
}


3. connect to mssql2008

The code is as follows:


$ Result = sqlsrv_query ($ con, $ query );
While ($ row = sqlsrv_fetch_array ($ result ))
{
$ Variable name = $ row ["field name"];
}


Sqlsrv library is not included in php5.3 and later versions. So we need to download it from Microsoft.

4. close the connection

There is no difference in this: odbc_close (); and mssql_close () and sqlsrv_close ();

Finally, I learned that php is less connected to mssql than to mssql, but it is enough. For specific functions, refer to the official php Manual.

Skip; extension = remove the semicolon before php_pdo_odbc.dll, which method is used to connect to mssql. Please restart the service to make it take effect ....

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.