Common Operations for connecting php to the MSSQL database

Source: Internet
Author: User
Tags odbc connection
Welcome to the Linux community forum and interact with 2 million technical staff to enter the database query in four steps. 1. Establish a connection. 2. Enter the query code. 3. Create a query and retrieve data. 4. Close the connection. Php has several considerations for connecting to the mssql database, especially when multiple versions, 32-bit and 64-bit of mssql are different. First, the php. ini file

Welcome to the Linux community forum and interact with 2 million technical staff> there are four steps for accessing the database: 1. Establish a connection. 2. Enter the query code. 3. Create a query and retrieve data. 4. Close the connection. Php has several considerations for connecting to the mssql database, especially when multiple versions, 32-bit and 64-bit of mssql are different. First, the php. ini file

Welcome to the Linux community forum and interact with 2 million technicians>

Database query involves four steps: 1. Establish a connection. 2. Enter the query code. 3. Create a query and retrieve data. 4. Close the connection.

Php has several considerations for connecting to the mssql database, especially when multiple versions, 32-bit and 64-bit of mssql are different.

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.

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

2. Connect to mssql2000

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

3. Connect to mssql2008

$ 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

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

3. Create a query and retrieve data

1. odbc

$ Result = odbc_do ($ con, $ query );

While (odbc_fetch_row ($ result ))

{

$ Variable name = odbc_result ($ result, "field name ");

}

2. Connect to mssql2000

$ Result = mssql_query ($ con, $ query );

While ($ row = mssql_fetch_array ($ result ))

{

$ Variable name = $ row ["field name"];

}

3. Connect to mssql2008

$ 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 php official manual or oschina's php Chinese document.

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.