Several ways to connect MSSQL database with PHP _php tutorial

Source: Internet
Author: User
First, in the php.ini file; Extension=php_pdo_mssql.dll, extension=php_pdo_odbc.dll the semicolon in front of the removal, corresponding to which way to connect the MSSQL. Note To restart the service for it to take effect.

First, establish a connection

1. ODBC

First, set up ODBC on the server where the PHP program resides. There are differences between 32-bit and 64-bit operating systems. 32-bit from the management tool in the Control Panel data Source (ODBC) is directly established, 64-bit to run C:\Windows\SysWOW64\odbcad32.exe

Set from this side. Note: Only the database server is 32, and the data source sets the server to be both 32-bit and 64-bit. As long as two servers establish a consistent number of data source bits.

The following is the ODBC build connection code.

Copy the Code code as follows:
$con = Odbc_connect (' ODBC name ', ' username ', ' password ');

2. Connecting mssql2000

Copy the Code code as follows:
$con = Mssql_connect (' Database address ', ' username ', ' password ');

3. Connecting mssql2008
Copy the Code code as follows:
$connectionInfo = Array ("UID" + user name, "PWD" + = password, "database" + "DB name");
$con = sqlsrv_connect (database address, $connectionInfo);

Second, enter the query code

This is the same, can be directly written, or can be verified from the MSSQL after copying. The simple point is to assign a SQL statement to a variable.

Similar to the following code
Copy the Code code as follows:
$query = "SELECT Top 12 * Database name ORDER BY id DESC";

Third, establish the query and take out the data

1. ODBC
Copy the Code code as follows:
$result = Odbc_do ($con, $query);
while (Odbc_fetch_row ($result))
{
$ Variable name = Odbc_result ($result, "field name");
}

2. Connecting mssql2000
Copy the Code code as follows:
$result = Mssql_query ($con, $query);
while ($row =mssql_fetch_array ($result))
{
$ Variable name = $row ["field name"];
}

3. Connecting mssql2008
Copy the Code code as follows:
$result = sqlsrv_query ($con, $query);
while ($row = Sqlsrv_fetch_array ($result))
{
$ Variable name = $row ["field name"];
}

The SQLSRV library is not included in the php5.3 and later versions. So I want to download it from Microsoft.

Four, close the connection

This is no different from odbc_close (), and Mssql_close () and sqlsrv_close ();

Finally realized: PHP connection MSSQL is less than the function of the connection MSSQL, but also enough. Specific functions can refer to the official PHP manual

http://www.bkjia.com/PHPjc/326589.html www.bkjia.com true http://www.bkjia.com/PHPjc/326589.html techarticle first, in the php.ini file; Extension=php_pdo_mssql.dll, extension=php_pdo_odbc.dll the semicolon in front of the removal, corresponding to which way to connect the MSSQL. Note To restart the service for it to 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.