Home > Backend Development > PHP Problem > How to change the database in php project

How to change the database in php project

PHPz
Release: 2023-04-05 13:41:00
Original
583 people have browsed it

PHP is a very popular server-side scripting language that can be used to develop web applications. When developing PHP projects, you often need to interact with databases. This requires us to learn how to change the database.

In this article, we will explain how to change the database. We will discuss it from the following aspects:

  1. Connect to database
  2. Create database
  3. Modify database table
  4. Delete database table
  5. Add new data
  6. Modify data
  7. Delete data
  8. Database backup
  9. Database restore
  10. Connect to database

Connecting to the database is the basis for all database operations. In PHP, we can use extension libraries such as mysqli or PDO to connect to the MySQL database. There are two common connection methods:

mysqli extension:

$host = "localhost"; //数据库服务器名称
$username = "root"; //连接mysql用户名
$password = "123456"; //连接mysql密码
$db_name = "test"; //数据库名称
 
// 创建一个连接到MySQL服务器的mysqli对象
$mysqli = new mysqli($host, $username, $password, $db_name);
 
// 检查连接是否成功
if ($mysqli->connect_error) {
    die("连接失败: " . $mysqli->connect_error);
}
Copy after login

PDO extension:

$host = "localhost"; //数据库服务器名称
$username = "root"; //连接mysql用户名
$password = "123456"; //连接mysql密码
$db_name = "test"; //数据库名称
 
// PDO连接数据库
try {
    $dsn = "mysql:host={$host};dbname={$db_name}";
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo "连接失败: " . $e->getMessage();
}
Copy after login
  1. Create database

If If the database to be used has not been created on the MySQL server, you need to create a new database first. You can create a new database in MySQL using the following statement:

CREATE DATABASE test;
Copy after login

In PHP, you can use the query() method in mysqli or PDO extension library to execute the above statement. The syntax of mysqli extension is as follows:

//创建数据库
$sql = "CREATE DATABASE test";
if ($mysqli->query($sql) === true) {
    echo "数据库创建成功";
} else {
    echo "Error创建数据库: " . $mysqli->error;
}
Copy after login

The syntax of PDO extension is as follows:

//创建数据库
$sql = "CREATE DATABASE test";
if ($pdo->exec($sql) !== false) {
    echo "数据库创建成功";
} else {
    echo "Error创建数据库";
}
Copy after login
  1. Modify database table

In PHP, we can use ALTER TABLE statement to modify the structure of a database table. The following is an example of the ALTER TABLE statement:

ALTER TABLE Persons
ADD YearOfBirth INT;
Copy after login

In the above example, we added a new column named "YearOfBirth" and type "INT" to the "Persons" table. In PHP, you can use mysqli or PDO extension library to execute such statements.

The syntax of mysqli extension is as follows:

$sql = "ALTER TABLE Persons ADD YearOfBirth INT";
 
if ($mysqli->query($sql) === true) {
    echo "修改成功";
} else {
    echo "Error修改数据库:" . $mysqli->error;
}
Copy after login

The syntax of PDO extension is as follows:

$sql = "ALTER TABLE Persons ADD YearOfBirth INT";
 
if ($pdo->exec($sql) !== false) {
    echo "修改成功";
} else {
    echo "Error修改数据库";
}
Copy after login
  1. Delete database table

When we need When deleting a database table, you can use the following command:

DROP TABLE table_name;
Copy after login

In PHP, you can use the query() method of mysqli or PDO extension to execute the above command. The following is an example:

$sql = "DROP TABLE Persons";
 
if ($mysqli->query($sql) === true) {
    echo "数据表删除成功";
} else {
    echo "Error删除数据表:" . $mysqli->error;
}
Copy after login

The syntax of the PDO extension is similar and will not be repeated here.

  1. Add new data

In PHP, you can use the INSERT statement to add a new piece of data to the database. The following is an example:

$sql = "INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('John', 'Doe', '35')";
 
if ($mysqli->query($sql) === true) {
    echo "数据插入成功";
} else {
    echo "Error插入数据:" . $mysqli->error;
}
Copy after login

In the above example, we inserted a record into the "Persons" table.

  1. Modify data

In PHP, you can use the UPDATE statement to modify a piece of data in the database. The following is an example:

$sql = "UPDATE Persons SET Age='36'
WHERE FirstName='John' AND LastName='Doe'";
 
if ($mysqli->query($sql) === true) {
    echo "数据修改成功";
} else {
    echo "Error修改数据:" . $mysqli->error;
}
Copy after login

In the above example, we updated the value of the "Age" field of the record whose "FirstName" is "John" and "LastName" is "Doe" to "36".

  1. Delete data

In PHP, you can use the DELETE statement to delete a piece of data in the database. The following is an example:

$sql = "DELETE FROM Persons WHERE FirstName='John' AND LastName='Doe'";
 
if ($mysqli->query($sql) === true) {
    echo "数据删除成功";
} else {
    echo "Error删除数据:" . $mysqli->error;
}
Copy after login

In the above example, we deleted the record with "FirstName" as "John" and "LastName" as "Doe".

  1. Database Backup

Before making important updates or additions, we should back up the database first. In MySQL, we can use the mysqldump command to create a database backup. The following is an example:

mysqldump -u root -p database_name > backup.sql
Copy after login

In the above example, we used the mysqldump command to export the contents of the "database_name" database to the "backup.sql" file.

  1. Database recovery

If we need to restore the backup of the database, we can use the following command:

mysql -u root -p database_name < backup.sql
Copy after login

The above command will restore "backup.sql" The contents of the file are restored into the "database_name" database.

In actual development, PHP projects interact with databases very frequently. We need to learn database operations in the PHP language to better develop PHP Web applications. In this article, we explain how to connect to the database, create, modify, and delete database tables, add, modify, and delete data, as well as database backup and recovery methods. We hope it will be helpful to readers.

The above is the detailed content of How to change the database in php project. For more information, please follow other related articles on the PHP Chinese website!

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