SoFunction
Updated on 2025-03-09

Composition and summary of common code segments for PHP operation MySQL

This article introduces you to the code segments that are commonly used in the actual development of PHP websites. All codes are executed reliably. This article will continue to be updated! ! !

1. Insert data table into the database

<?php

$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("【Database Name】", $con);//Select MySQL database
$sql = "CREATE TABLE abc

(

id int NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

openid varchar(32),

nickname varchar(32),

sex varchar(8)

)";//Create a data table with the name abc. The id cannot be empty and automatically incremented and set as the primary key
mysql_query($sql,$con);//Execute a MySQL statement
mysql_close($con);//Close mysql connection
?>

2. Insert new records into the database table

<?php

$datatime = date("Y-m-d H:i:s",time());//Get time
$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("【Database Name】", $con);//Select MySQL database
mysql_query("SET NAMES 'UTF8'");//Set encoding (solve the problem of inserting Chinese garbled code)
mysql_query("INSERT INTO 【Data table name】 (openid, add_time, nickname)

VALUES ('123', '$datatime', 'abc')");//Insert a new record
mysql_close($con);//Close mysql connection
?>

3. Read all contents of the data table

<?php

$con = mysql_connect("【Database Address】","Database username]","【Database Password】");//Create MySQL connection
mysql_select_db("【Database Name】", $con);//Select MySQL database
$result = mysql_query("SELECT * FROM 【Data table name】");//Get all data in the data table
while($row = mysql_fetch_array($result)){//Get a row from the result set as an associative array. If there are no more rows, return false
    echo $row['openid']."
";//The values ​​of all openid fields in the output table
}

mysql_close($con);//Close mysql connection
?>

4. Read the matching data of the data table

<?php

$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("【Database Name】", $con);//Select MySQL database
$result = mysql_query("SELECT * FROM 【Data table name】 WHERE openid='123'");//Get the data row with openid=123 in the data table
while($row = mysql_fetch_array($result)){//Get a row from the result set as an associative array. If there are no more rows, return false
    echo $row['nickname']."
";//The values ​​of all openid fields in the output table
}

mysql_close($con);//Close mysql connection
?>

5. Modify the data in the database table

<?php

$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("【Database Name】", $con);//Select MySQL database
mysql_query("UPDATE 【Data table name】 SET nickname='new' WHERE openid='123'");//Update nickname field of id=123 record line
mysql_close($con);//Close mysql connection
?>

6. Delete records from the data table

<?php

$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("Database Name", $con);//Select MySQL database
mysql_query("DELETE FROM Data table name WHERE openid='123'");//Delete a line of record with openid=123
mysql_close($con);//Close mysql connection
?>

7. Delete data tables from database

<?php

$con = mysql_connect("【Database Address】","【Database Username】","【Database Password】");//Create MySQL connection
mysql_select_db("Database Name", $con);//Select MySQL database
$sql = "DROP TABLE abc";//Delete the data table named abc
mysql_query($sql,$con);//Execute a MySQL statement
mysql_close($con);//Close mysql connection
?>

The PHP Data Object (PDO) extension defines a lightweight consistent interface for PHP accessing databases. Provides a data access abstraction layer, which means that no matter which database is used, the same functions (methods) can be used to query and obtain data.
PDO is released with PHP5.1 and can also be used in the PECL extension of PHP5.0 and cannot run in previous PHP versions.
Here is an example to illustrate the usage of PDO:

<?php

$host = "【Database Address】";

$username = "【Database Username】";

$password = "【Database Password】";

$dbname = "【Database Name】";

//Put the code to be executed into the try block. If an exception occurs in a statement during the execution of these codes, the program will jump directly to the catch block, and $e collects error information and displays.
try {

    $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);//Create a connection
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set PDO error mode, used to throw exceptions
    $sql = "CREATE TABLE abc (

    id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,

    openid varchar(32) NOT NULL,

    nickname varchar(32) NOT NULL,

    sex varchar(8) NOT NULL

    )";//Create a data table with the name abc. The id cannot be empty and automatically incremented and set as the primary key
    $conn->exec($sql);//Use exec() without result returns
}

catch(PDOException $e){

    echo $sql . "
" . $e->getMessage();//Show exception information
}

$conn = null;//Close the connection
?>

If the environment allows, use PDO as much as possible to perform MySQL database operations.

This is the article about sorting out and summarizing common code snippets for PHP operation MySQL. For more related PHP operation MySQL content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!