Home > Backend Development > PHP Problem > Steps to access database in php

Steps to access database in php

(*-*)浩
Release: 2023-02-23 15:24:01
Original
5138 people have browsed it

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/>
Copy after login

Select the database to connect

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

Set the encoding format

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

Write sql statement

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

Execute the sql statement and receive the execution result

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

Put the result Enter the array

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

Return the results obtained from the database to the front end

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

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!

Related labels:
php
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