Can php connect to sql?

藏色散人
Release: 2023-04-06 13:56:01
Original
5987 people have browsed it

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.

Can php connect to sql?

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);
?>
Copy after login

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); //选择数据库
?>
Copy after login

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.

Can php connect to sql?

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"连接失败";
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template