Summary of three ways to connect to SQL Server in PHP

Source: Internet
Author: User
Tags microsoft drivers for php for sql server sql server driver
Refer to the PHP manual for a summary of the PHP connection to the SQL Server family. There are three main ways of doing this:

First, through the Mssql_ series functions, mainly for the following version of php5.3 and sqlserver2000 and later use. in php.ini; extension=php_mssql.dll;extension=php_msql.dll;extension=php_pdo_mssql.dll; extension=php_pdo_. The semicolon in front of the Odbc.dll is removed.

Second, through the SQLSRV_ series functions, mainly for more than php5.3 version and SQL Server 2005 and later use. You need to download the installation of Microsoft Drivers for PHP for SQL Server driver, address: https://msdn.microsoft.com/library/dn865013.aspx. Download and then unzip the PHP corresponding to the EXT directory. Then open the php.ini file and add the configuration after extension

Extension=php_pdo_sqlsrv_53_ts.dll
Extension=php_sqlsrv_53_ts.dll

Restart Apache and check phpinfo () to make sure Apache has supported sqlsrv. As shown in the following:

and install Sqlncli.msi, this file is the one that assists the Windows environment to access the database server where SQL Server resides

Third, connect the SQL Server series by ODBC mode. ODBC series extensions need to be opened in php.ini. See in the Phpinfo function

Here is the implementation code:


$serverName = "localhost";
$connectionInfo = Array ("Database" = "Jxc_erp", "UID" = "sa", "PWD" = "123456");
$conn = Sqlsrv_connect ($serverName, $connectionInfo);
if ($conn = = = False) {
Die (Print_r (Sqlsrv_errors (), true));
// }

$sql = "SELECT * FROM dbo. A_php ";
$stmt = sqlsrv_query ($conn, $sql);
if ($stmt = = = False) {
Die (Print_r (Sqlsrv_errors (), true));
// }


while ($row = Sqlsrv_fetch_array ($stmt))
{echo $row [0]. " -----". $row [1]."
";}

$con = Odbc_connect (' jxc_erp ', ' sa ', ' 123456 ');
$query = "SELECT * FROM dbo. A_php ";
$result = Odbc_do ($con, $query);
while (Odbc_fetch_row ($result))
{
$list = Odbc_result ($result, "id"); Print_r ($list); Echo '
';
}


$server = "localhost"; Server IP address, if local, can be written as localhost
$uid = "sa"; User name
$pwd = "123456"; Password
$database = "Jxc_erp"; Database name

Making a database connection
$conn =mssql_connect ($server, $uid, $pwd) or Die ("Connect failed");
mssql_select_db ($database, $conn);

Execute Query statement
$query = "SELECT * from a_php";
$row =mssql_query ($query);

Print output Query Results
while ($list =mssql_fetch_array ($row))
// {
Print_r ($list);
echo "
";
// }

?>

  • 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.