Home  >  Article  >  Backend Development  >  How to connect to MariaDB database using PDO

How to connect to MariaDB database using PDO

王林
王林Original
2023-07-28 14:49:431250browse

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:

dde77b206903f6ce48d52545c6c806cb

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:
    ced9ee379b59cee6c0038fde90865a09fetch()) {
    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:
    374efe54e3a8ceaed7ca25a89b2f2b57prepare($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:
    724788188b0133ab3a418d767baa2c5c

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:

c323ddddcb3fc3329e7d573296a55b47

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!

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