Home >Backend Development >PHP Problem >How to connect php to sql

How to connect php to sql

王林
王林Original
2019-09-21 17:59:212785browse

How to connect php to sql

Refer to the php manual to make a summary of the php connection sql server series. There are five main ways:

1. Through mssql_ series functions, mainly used for versions below php5.3 and sqlserver2000 and above. .

In php.ini, add ;extension=php_mssql.dll, ;extension=php_mssql.dll, ;extension=php_pdo_mssql.dll

;extension=php_pdo_odbc.dllJust remove the semicolon in front of it.

2. Through the sqlsrv_ series of functions, it is mainly used for versions above php5.3 and sql server 2005 or above. .

You need to download and install the Microsoft Drivers for PHP for SQL Server driver. After downloading, unzip it and put it in the ext directory corresponding to php, 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, check phpinfo(), and ensure that apache has supported sqlsrv. As shown in the figure below:

How to connect php to sql

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

How to connect php to sql

#3. Connect to the sql server series through odbc. You need to enable the odbc series extension in php.in. As shown below:

How to connect php to sql

4. Connect to sqlserver through PDO. Enable the PDO extension in php.ini. You can view it in phpinfo

How to connect php to sql

5. Connect through COM

The following is the implementation code:

<?php

// $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]."<br/>";}

$con = odbc_connect(&#39;JXC_ERP&#39;,&#39;sa&#39;,&#39;123456&#39;);
$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 &#39;<br/>&#39;;
}


// $server ="localhost";  //服务器IP地址,如果是本地,可以写成localhost
// $uid ="sa";  //用户名
// $pwd ="123456"; //密码
// $database ="JXC_ERP";  //数据库名称
 
////进行数据库连接
// $conn =mssql_connect($server,$uid,$pwd) or die ("connect failed");
// mssql_select_db($database,$conn);
 
////执行查询语句
// $query ="select * from A_PHP";
// $row =mssql_query($query);
 
////打印输出查询结果
// while($list=mssql_fetch_array($row))
// {
       // print_r($list);
       // echo "<br>";
// }
 
//$conn = new PDO("sqlsrv:server=localhost;database=JXC_ERP","sa","123456");
//$sql = "select count(*) count from CKXS2";
//$res = $conn->query($sql);
//while ($row = $res->fetch()){
//print_r($row);
//}
//$conn = new Com("ADODB.Connection"); //实例化一个Connection对象 
//$connstr = "provider=sqloledb;datasource=.;uid=sa;pwd=123456;database=JXC_ERP;"; 
//$conn->Open($connstr); 
//$rs = new Com("ADODB.Recordset"); //实例化一个Recordcount对象 

//$rs->Open(&#39;select * from CKXS2 &#39;, $conn, 1, 1); 
//$count = $rs->RecordCount; 
//echo "共有{$count}条纪录<br />"; 
?>

Recommended tutorial: PHP video tutorial

The above is the detailed content of How to connect php to sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn