Home >Backend Development >PHP Problem >How to change the database in php project
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:
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); }
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(); }
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;
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; }
The syntax of PDO extension is as follows:
//创建数据库 $sql = "CREATE DATABASE test"; if ($pdo->exec($sql) !== false) { echo "数据库创建成功"; } else { echo "Error创建数据库"; }
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;
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; }
The syntax of PDO extension is as follows:
$sql = "ALTER TABLE Persons ADD YearOfBirth INT"; if ($pdo->exec($sql) !== false) { echo "修改成功"; } else { echo "Error修改数据库"; }
When we need When deleting a database table, you can use the following command:
DROP TABLE table_name;
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; }
The syntax of the PDO extension is similar and will not be repeated here.
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; }
In the above example, we inserted a record into the "Persons" table.
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; }
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".
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; }
In the above example, we deleted the record with "FirstName" as "John" and "LastName" as "Doe".
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
In the above example, we used the mysqldump command to export the contents of the "database_name" database to the "backup.sql" file.
If we need to restore the backup of the database, we can use the following command:
mysql -u root -p database_name < backup.sql
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!