Home>Article>Backend Development> Steps to access database in php
There are 7 steps in total for php to access the database. The following is a detailed description
Connecting to the database server(recommended learning:PHP video tutorial)
//参数1:数据库所在服务器的域名
//参数2:登录数据库的用户名
//参数3:登录数据库的密码
$con = mysqli_connect("localhost:3306","root","")
or die(mysqli_connect_error());
//die终止程序代码执行,并输出错误信息
Select the database to connect
$dataBase = mysqli_select_db($con,"zzq1802")
or die(mysqli_connect_error());
Set the encoding format
$dataBase = //mysqli_query:执行语句
mysqli_query($con,"set names utf8");
Write sql statement
$sql = "SELECT * FROM student";
Execute the sql statement and receive the execution result
$result = mysqli_query($con,$sql);
Put the result Enter the array
$arr = array();
while($row = mysqli_fetch_assoc($result)){
array_push($arr,$row);
}
Return the results obtained from the database to the front end
$arr = array();
echo json_encode($arr);
The above is the detailed content of Steps to access database in php. For more information, please follow other related articles on the PHP Chinese website!