Home  >  Article  >  Backend Development  >  How to interact with database using PHP7

How to interact with database using PHP7

PHPz
PHPzOriginal
2023-03-31 10:37:251017browse

PHP7 is a popular programming language used for developing web applications. Databases are an important part of web applications. In this article, we will discuss how to interact with a database using PHP7.

  1. PHP7 interacts with MySQL database

MySQL is a common relational database management system. PHP7 can interact with MySQL database through MySQLi or PDO extension.

The MySQLi extension provides a faster and more reliable way to communicate with MySQL databases. Here are some sample codes:

//Connect to the database
$conn = mysqli_connect("localhost", "Username", "Password", "Database Name");
//Query data
$result = mysqli_query($conn, "SELECT * FROM table name");
//Output results
while ($row = mysqli_fetch_assoc($result)) {

echo $row['字段名'];

}
//Close the connection
mysqli_close($conn);

PDO extension provides a more flexible and scalable API that can connect to multiple database types. Here are some sample codes:

//Connect to the database
$conn = new PDO("mysql:host=localhost;dbname=database name", "username", "password");
//Query data
$result = $conn->query("SELECT * FROM table name");
//Output results
while ($row = $result->fetch(PDO ::FETCH_ASSOC)) {

echo $row['字段名'];

}
//Close the connection
$conn = null;

  1. PHP7 interacts with the MongoDB database

MongoDB is a non-relational database. Unlike traditional relational databases, MongoDB stores data in the form of documents. PHP7 can interact with the MongoDB database through the MongoDB extension.

Here are some sample codes:

//Connect to the database
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
/ /Query data
$query = new MongoDB\Driver\Query([]);
$cursor = $manager->executeQuery("database name.collection name", $query);
// Output results
foreach ($cursor as $document) {

print_r($document);

}

  1. PHP7 interacts with Redis database

Redis is a kind of memory Database, also known as NoSQL database. PHP7 can interact with the Redis database through the PHP Redis extension.

Here are some sample codes:

//Connect to the database
$redis = new Redis();
$redis->connect('127.0.0.1', 6379 );
//Set value
$redis->set('key', 'value');
//Get value
echo $redis->get('key') ;

Summary

PHP7 provides a variety of extensions that can interact with a variety of databases. When interacting with the database, you need to confirm which database you are connected to and use the corresponding extension. The method of connecting to the database may vary depending on the database type, but generally you need to specify the corresponding parameters, such as user name, password, host name, port number, etc. When using connections, you need to pay attention to closing the connection to release resources.

The above is the detailed content of How to interact with database using PHP7. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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