Home > Backend Development > PHP Tutorial > Practical Guide to Compiling and Installing PHP Extensions: PDO MySQL

Practical Guide to Compiling and Installing PHP Extensions: PDO MySQL

WBOY
Release: 2024-03-08 08:28:01
Original
1164 people have browsed it

PHP扩展编译安装实操指南:PDO MySQL篇

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.

1. Download the source code package

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.

2. Compile and install the PHP extension

  1. Decompress the downloaded PHP source code package and enter the decompressed directory.
  2. Execute the following command in the terminal to enter the PHP extension directory:
cd ext/pdo_mysql
Copy after login
  1. Execute the following command to compile and install:
phpize
./configure
make
make install
Copy after login

3. Configuration PHP.ini file

  1. Open the php.ini file and add the following configuration:
extension=pdo_mysql.so
Copy after login
  1. Save the php.ini file and restart the PHP service:
sudo service php-fpm restart
Copy after login

4. Test the PDO MySQL extension

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();
}
Copy after login

5. Run the test script

Save the above code as test.php and execute the following command in the terminal:

php test.php
Copy after login

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!

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