Home>Article>Backend Development> Steps to access database in php

Steps to access database in php

(*-*)浩
(*-*)浩 Original
2019-09-04 09:35:23 5004browse

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!

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