Summary of three methods for connecting php to sqlserver

Source: Internet
Author: User
Tags microsoft drivers for php for sql server sql server driver
For a summary of the three methods for connecting php to sqlserver, refer to the php manual to make a summary of connecting php to the SQL server series. There are three main methods:

1. mssql _ series functions are mainly used for versions below php5.3 and sqlserver2000 and later. In php. ini, remove the semicolon before extension = php_mssql.dll, extension = php_msql.dll, extension = php_pdo_mssql.dll, and extension = php_pdo_odbc.dll.

II. sqlsrv _ series functions are mainly used for php5.3 and later versions and SQL server 2005 and later versions. Download and install the Microsoft Drivers for PHP for SQL Server driver at https://msdn.microsoft.com/library/dn865013.aspx. Download and decompress the package to the ext directory corresponding to php. 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 ensure that apache supports sqlsrv. As shown in:

And install sqlncli. msi. this file helps the windows environment access the database server where SQL server is located.

3. connect to the sqlserver series through odbc. You need to enable odbc extension in php. ini. In the phpinfo function, you can see

The following 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', '123 ');
$ 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"; // The IP address of the server. if the IP address is local, it can be written as localhost.
// $ Uid = "sa"; // User name
// $ Pwd = "123456"; // password
// $ Database = "JXC_ERP"; // database name

//// Connect to the database
// $ Conn = mssql_connect ($ server, $ uid, $ pwd) or die ("connect failed ");
// Mssql_select_db ($ database, $ conn );

//// Execute the query statement
// $ Query = "select * from A_PHP ";
// $ Row = mssql_query ($ query );

//// Print the output query result
// While ($ list = mssql_fetch_array ($ row ))
//{
// Print_r ($ list );
// Echo"
";
//}

?>

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.