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

Steps to access database in php

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

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:数据库所在服务器的域名<br/>//参数2:登录数据库的用户名<br/>//参数3:登录数据库的密码<br/>$con = mysqli_connect("localhost:3306","root","")<br/>or die(mysqli_connect_error());<br/>//die终止程序代码执行,并输出错误信息<br/>

Select the database to connect

$dataBase = mysqli_select_db($con,"zzq1802")<br/>or die(mysqli_connect_error());<br/>

Set the encoding format

$dataBase = //mysqli_query:执行语句<br/>mysqli_query($con,"set names utf8");<br/>

Write sql statement

$sql = "SELECT * FROM student";<br/>

Execute the sql statement and receive the execution result

$result = mysqli_query($con,$sql);<br/>

Put the result Enter the array

$arr = array();<br/>while($row = mysqli_fetch_assoc($result)){<br/>    array_push($arr,$row);<br/>}<br/>

Return the results obtained from the database to the front end

$arr = array();<br/>echo json_encode($arr);<br/>

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