php can be connected to sql. There are two ways for PHP to connect to the SQLServer database, namely: 1. Connect through ADO; 2. Connect through the "mssql_connect()" function.
PHP often cooperates with MySQL database, but sometimes it also cooperates with SQL Server database. So how to connect to SQL Server database in PHP?
Can php be connected to sql?
php can be connected to sql. There are two ways for PHP to connect to the SQLServer database: one is to connect through ADO; the other is to connect through the mssql_connect() function. These two connection methods are introduced below.
1. Connecting to SQL Server database through ADO
Using ADO to establish a connection with the SQL Server database is similar to the method of using ADO to establish a connection with the Access database. Establishing a connection is implemented in the conn.php file of this example. The code is as follows:
<?php $conn=new com("adodb.connection"); $connstr="provider=sqloledb;data source=localhost;uid=sa;pwd=;database=db_online"; $conn->open($connstr); ?>
2. Connect to SQL Server database through mssql_connect() function
To establish a connection with SQL Server database through mssql_connect() function, you should first configure the php.ini file, remove the comment before extension=php_mssql.dll, and then restart Apache server. The code to establish a connection with the database through the mssql_connect() function is as follows:
<?php $conn=mssql_connect("localhost","sa",""); //建立与SQL Server 数据库的连接 mssql_select_db("db_online ",$conn); //选择数据库 ?>
For example, the following uses the mssql_connect() function to connect to the SQL Server database, and the connection result is as shown in the figure.
Connect to SQL Server database
The code to connect to SQL Server database is as follows:
<?php $conn=mssql_connect("LLL-SC08Z7OW0V9","sa",""); //建立与SQL Server 数据库的连接 $select=mssql_select_db("master",$conn); //选择数据库 if($select){ echo"连接成功"; }else{ echo"连接失败"; } ?>
The above is the detailed content of Can php connect to sql?. For more information, please follow other related articles on the PHP Chinese website!