Get the App
SLTechnology News&Howtos  ›  Database  › 

Six steps of connecting to mysql Database with PHP

Shulou Source: shulou.com Published: 2022-06-01 19:30:19 09月26日 Update

This article mainly introduces the six steps of PHP to connect to mysql database, the contents of the article are carefully selected and edited by the author, with a certain pertinence, and is of great significance to everyone's reference, the following is to understand the six steps of PHP to connect to mysql database together with the author.

/ / 1. Get connection

$conn=mysql_connect ('localhost','root','root')

If (! $conn) {

Die ("connection failed" .MySQL _ error ())

/ / 2. Select a database

Mysql_select_db ("test")

/ / 3. Set operation code (recommended)

Mysql_query ("set naems utf8"); / / make sure that the php program operates according to utf8

/ / 4. Send instruction sql (ddl (data definition statement)

/ / dml (data manipulation language update,insert,delete)

/ / dql (select), dtl (data transaction statement rollback commit...)

$sql= "select * from user1"

$res=mysql_query ($sql,$conn); / / if you don't add $conn, use the previous connection

/ / $res points to memory

/ / 5. Accept the returned results and process them.

While ($row=mysql_fetch_row ($res)) {

/ / $row is an array

Echo ""

Foreach ($row as $key = > $val) {

Echo "--$val"

}

Echo ""

}

/ / 6. Release resources and close the connection

Mysql_free_result ($res)

Mysql_close ($conn); / / recommended

After reading the above six steps for PHP to connect to mysql database, many readers must have some understanding. If you need to get more industry knowledge and information, you can continue to follow our industry information column.

Tags: Data database steps suggestions author industry statements transactions information memory content that is meaning instructions directions arrays articles more friends columns Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Linux Microsoft OPPO Reno MariaDB