PHP Extension Compilation and Installation Practical Guide: PDO MySQL
With the development of the Internet and database technology, PHP, as a widely used server-side scripting language, The integration with databases such as MySQL is getting closer and closer. In PHP, PDO is a database access abstraction layer that can effectively provide a unified access interface to various databases. Using PDO can make PHP applications connect and operate databases more flexibly and efficiently. This article will introduce how to compile and install the PDO MySQL extension through practical operations, so that you can have a deeper understanding of the combination of PHP and database.
First, we need to download the PHP source code package and MySQL development package, as well as the source code of the PDO MySQL driver. These resources can be obtained from the official website or various open source mirror sites.
cd ext/pdo_mysql
phpize ./configure make make install
extension=pdo_mysql.so
sudo service php-fpm restart
Now that you have successfully compiled and installed the PDO MySQL extension, let’s write a simple PHP script to test the connection and query functions of PDO MySQL. .
<?php $dsn = 'mysql:host=localhost;dbname=test'; $user = 'root'; $password = '123456'; try { $pdo = new PDO($dsn, $user, $password); $stmt = $pdo->query('SELECT * FROM users'); while ($row = $stmt->fetch()) { echo $row['username'] . " "; } } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }
Save the above code as test.php and execute the following command in the terminal:
php test.php
If everything goes well, you will see List of user names queried in the database. This indicates that you have successfully installed and used the PDO MySQL extension.
Through the practical guide of this article, you can have a deeper understanding of the compilation and installation process of PHP extensions, and how to use the PDO MySQL extension to interact with the database. I hope this article will be helpful to you and make you more comfortable when developing PHP applications!
The above is the detailed content of Practical Guide to Compiling and Installing PHP Extensions: PDO MySQL. For more information, please follow other related articles on the PHP Chinese website!