Home > Backend Development > PHP Tutorial > How to use MySQL functions in PHP

How to use MySQL functions in PHP

王林
Release: 2023-05-18 13:52:01
Original
1656 people have browsed it

In PHP applications, database management is crucial. Integration with MySQL database is one of the skills that PHP developers should master. MySQL is a popular open source database management system for which PHP provides extensive support. PHP programmers can use MySQL functions to integrate with the MySQL database to implement operations such as data insertion, query, update and deletion.

In this article, we will take a deep dive into how to use MySQL functions in PHP. We will cover the following topics:

  1. Connecting to a MySQL database
  2. Executing queries
  3. Inserting data
  4. Updating data
  5. Deleting Data

Connecting to a MySQL database

Connecting to a MySQL database in PHP is easy. You can use built-in mysqli functions or PDO classes.

When using the mysqli function, you need to use the following command to create a MySQL connection:

$conn = mysqli_connect($servername, $username, $password, $dbname);
Copy after login

Among them:

  • ##$servername: MySQL server name
  • $username: MySQL username
  • $password: MySQL user password
  • $dbname : The name of the database to be connected to
This function returns a connection object

$conn. If the connection fails, FALSE will be returned.

When using the PDO class, you can use the following command to create a MySQL connection:

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
Copy after login

Where:

  • $servername: MySQL server name
  • $username: MySQL username
  • $password: MySQL user password
  • $dbname : The name of the database to be connected to
This function returns a connection object

$conn. If the connection fails, a PDOException will be thrown.

After successfully connecting to the MySQL database, we can now start performing query, insert, update and delete operations.

Execute queries

There are two main ways to execute queries in PHP:

    Use the mysqli function to execute queries
  1. Use the PDO class to execute queries
Use the mysqli function to execute the query

The following is an example:

$sql = "SELECT id, name, age FROM users";
$result = mysqli_query($conn, $sql);
Copy after login

This code executes the SELECT query and stores the results in

$result in variables. You can use the following code to display the results:

while($row = mysqli_fetch_assoc($result)) {
  echo "ID: " . $row["id"] . " Name: " . $row["name"] . " Age: " . $row["age"];
}
Copy after login

Use the PDO class to execute the query

The following is an example:

$sql = "SELECT id, name, age FROM users";
$result = $conn->query($sql);
Copy after login

This code executes the SELECT query and returns the results Stored in the

$result variable. You can use the following code to display the results:

foreach($result as $row) {
  echo "ID: " . $row["id"] . " Name: " . $row["name"] . " Age: " . $row["age"];
}
Copy after login

Insert data

There are two main methods of inserting data in PHP:

    Insert data using the mysqli function
  1. Use the PDO class to insert data
Use the mysqli function to insert data

The following is an example:

$sql = "INSERT INTO users (name, age) VALUES ('John Doe', 25)";
mysqli_query($conn, $sql);
Copy after login

This code will ## Insert a record into the #users

table, where name is John Doe and age is 25. Use the PDO class to insert data

The following is an example:

$sql = "INSERT INTO users (name, age) VALUES ('John Doe', 25)";
$conn->exec($sql);
Copy after login

This code will insert a record into the

users

table, wherename is John Doe and age is 25. Update data

There are two main ways to update data in PHP:

Use the mysqli function to update data
  1. Use the PDO class to update data
  2. Using the mysqli function to update data

The following is an example:

$sql = "UPDATE users SET age = 26 WHERE name = 'John Doe'";
mysqli_query($conn, $sql);
Copy after login

This code will update the

name in the

users table The age field for the record John Doe is 26. Using the PDO class to update data

The following is an example:

$sql = "UPDATE users SET age = 26 WHERE name = 'John Doe'";
$conn->exec($sql);
Copy after login

This code will update the

name

in the users table The age field of the record for John Doe is 26. Delete data

There are two main ways to delete data in PHP:

Use the mysqli function to delete data
  1. Use the PDO class to delete data
  2. Use the mysqli function to delete data

The following is an example:

$sql = "DELETE FROM users WHERE name = 'John Doe'";
mysqli_query($conn, $sql);
Copy after login

This code will delete the

name in the

users table Records for John Doe. Use the PDO class to delete data

The following is an example:

$sql = "DELETE FROM users WHERE name = 'John Doe'";
$conn->exec($sql);
Copy after login

This code will delete the

name

in the users table Records for John Doe. Conclusion

The use of MySQL functions in PHP is very convenient. The above examples are just some basic syntax. The application of MySQL functions gives us very powerful data processing capabilities. To use them, you need to first connect to a MySQL database. You can then perform various query, insert, update, and delete operations.

By reading this article, you should already know how to use MySQL functions in PHP. In practice, you can use them to create powerful database applications.

The above is the detailed content of How to use MySQL functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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