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"
";
//}
?>