How to connect to MariaDB database using PDO

王林
Release: 2023-07-28 15:42:02
Original
1264 people have browsed it

How to use PDO to connect to the MariaDB database

1. Introduction
PDO (PHP Data Objects) is a lightweight abstraction layer used in PHP to access the database. It provides developers with a unified set of interfaces to connect and operate different types of databases, including MariaDB, MySQL, SQLite, etc. This article will introduce how to use PDO to connect to the MariaDB database and give sample code.

2. Installation and Configuration
Before using PDO to connect to the MariaDB database, you need to ensure that the PDO extension and corresponding database driver have been installed in the PHP environment. In most PHP environments, the PDO extension is installed by default, but the corresponding database driver needs to be installed manually.

  1. Install PDO extension:
    You can find the following two lines of code in the php.ini file, make sure you have uncommented it (remove the preceding semicolon):
    extension=pdo.so
    extension=pdo_mysql.so
  2. Install the MariaDB driver:
    Execute the following command in the terminal to install the driver:
    sudo apt-get install php-mysql

3. Connect to the database
To connect to the MariaDB database, you need to provide the host name, user name, password and database name of the database. You can use the PDO constructor to create a PDO object and pass in the connection parameters. The sample code is as follows:

$host = 'localhost'; // Database host name
$dbname = 'test'; // Database name
$username = ' root'; // Username
$password = '123456'; // Password

try {

914d677647011384ad47d5641ba5d1b7

}
?>

In the above code, a DSN (Data Source Name) string is first created, which contains the host name and database name of the database. Then a PDO object is created through the PDO constructor and the connection information is passed in. If the connection is successful, "Successfully connected to the database" will be output, otherwise an error message indicating that the connection failed will be output.

4. Execute SQL statements
After connecting to the database, you can use the query() method of the PDO object to execute SQL statements. Here are a few common examples:

  1. Query data:
    $sql = "SELECT * FROM users";
    $stmt = $pdo-> ;query($sql);
    while ($row = $stmt->fetch()) {
    echo "ID: " . $row['id'] . ", username: " . $ row['username'] . ", password: " . $row['password'];
    }
    ?>

In the above code, a SQL statement, and then execute the query using the query() method of the PDO object and save the results in the $stmt variable. Query results can be read line by line through the fetch() method of $stmt.

  1. Insert data:
    $username = 'john';
    $password = '123456';
    $sql = "INSERT INTO users ( username, password) VALUES (:username, :password)";
    $stmt = $pdo->prepare($sql);
    $stmt->bindParam(':username', $username);
    $stmt->bindParam(':password', $password);
    $stmt->execute();
    ?>

In the above code , first define a SQL statement, use named placeholders to replace the real values, then use the prepare() method of the PDO object to compile the SQL statement into a prepared statement, and use the bindParam() method to bind the real value value. Finally, use the execute() method to execute the prepared statement.

  1. Update data:
    $sql = "UPDATE users SET password = 'new_password' WHERE id = 1";
    $stmt = $pdo-> ;query($sql);
    ?>

In the above code, a SQL statement is first defined, and the password of the user with id 1 is updated using the UPDATE statement.

5. End the connection
After using the database, you can use the null() method of the PDO object to close the database connection. The sample code is as follows:

$pdo = null; // Close the database connection
?>

The above are the basic steps for using PDO to connect to the MariaDB database and sample code. Through PDO, we can easily connect and operate the MariaDB database, improve development efficiency, and be able to adapt to different database types. I hope this article will help you understand and use PDO to connect to the MariaDB database.

The above is the detailed content of How to connect to MariaDB database using PDO. 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