Home > Backend Development > PHP Problem > How to add database through html and php

How to add database through html and php

PHPz
Release: 2023-04-06 13:00:02
Original
783 people have browsed it

With the development of the Internet, many websites have transformed from simple web page display to websites with dynamic interactive functions, which cannot be separated from the support of databases. This article will introduce how to add database through html and php.

1. What is a database?

Database refers to a collection of data organized according to certain rules. These data can be accessed and managed by different computer programs. Common database software includes MySQL, Oracle, SQL Server, etc.

2. How to create a database?

Normally, we need to install database software and connect to the database. In MySQL, we can use the following command:

CREATE DATABASE mydatabase;

where "mydatabase" is the name of the database we want to create.

3. How to create a data table?

Creating data tables in the database allows us to better organize the data. The following is a sample code to create a data table in MySQL:

CREATE TABLE customers (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
email VARCHAR( 50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

This code will create a data table named "customers" and define the fields in the data table (id, name, email and reg_date) and their attributes.

4. How to connect php to the database?

In php, we can use the following code to connect to the MySQL database:

// Assuming that we have installed and configured the database software, the following is to connect to Database code:
$conn = mysqli_connect("localhost", "username", "password");

//Detect connection
if (!$conn) {

die("Connection failed: " . mysqli_connect_error());
Copy after login

}
echo "Connected successfully";
?>

In the above code, "localhost" represents the address of the database server we are connected to, and "username" and "password" is the username and password of the database user we created.

5. How to insert data in PHP?

Inserting data through php is very simple. We can use the following code:

$sql = "INSERT INTO customers (name, email)
VALUES ('John Doe', 'john@example.com')" ;
if (mysqli_query($conn, $sql)) {

echo "New record created successfully";
Copy after login

} else {

echo "Error: " . $sql . "<br>" . mysqli_error($conn);</p>
<p>}<br>?></p>
<p>In the above code , we inserted a record into the "customers" data table. The "name" and "email" field values ​​of the record are "John Doe" and "john@example.com" respectively. </p>
<p>6. How to query data in PHP? </p>
<p>The following is a sample code for querying data using php: </p>
<p><?php<br/>$sql = "SELECT id, name, email FROM customers";<br/>$result = mysqli_query($conn, $sql);</p><p>if (mysqli_num_rows($result) > 0) {</p>
<pre class="brush:php;toolbar:false">// 输出数据
while($row = mysqli_fetch_assoc($result)) {
    echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
}
Copy after login

} else {

echo "0 结果";
Copy after login

}

mysqli_close($conn);
?>

In the above code, we use the "SELECT" statement to query "id", "name" and "email" from the "customers" data table field's data and returns a "$result" object. Next, we use the "mysqli_num_rows()" function to determine whether there are data query results. Finally, the data is output line by line through the "mysqli_fetch_assoc()" function.

7. Conclusion

Adding a database through HTML and PHP is one of the important means to achieve dynamic interaction on the website. With the above code, we can easily insert, query, update and delete data in the website.

The above is the detailed content of How to add database through html and php. 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