Source code compilation and installation of PHP PDO MySQL: detailed step analysis
In the process of building a Web server, PHP and MySQL are two indispensable and important components. PHP's PDO extension (PHP Data Objects) provides a flexible and efficient way to access databases in PHP. In this article, we will introduce in detail how to compile and install PHP using source code, enable PDO extension, and connect to MySQL database. The following are specific steps and code examples:
1. Preparation
Before starting the compilation and installation, make sure the following software has been installed:
2. Download the source code package
Download the latest Version of PHP source code package:
wget https://www.php.net/distributions/php-8.1.3.tar.gz
Download the latest version of PDO MySQL driver source code package:
wget https://pecl.php.net/get/PDO_MYSQL
3. Compile and install PHP
Decompress the PHP source code package:
tar -zxvf php-8.1.3.tar.gz
Enter the decompressed PHP directory and start compilation and installation:
cd php-8.1.3 ./configure --prefix=/usr/local/php --with-zlib --with-libxml make sudo make install
4. Enable PDO extension
Extract the downloaded PDO MySQL driver source code package:
tar -zxvf PDO_MYSQL
Enter the decompressed PDO MySQL directory, start the installation:
cd PDO_MYSQL /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config make sudo make install
Enable PDO extension in the PHP configuration file:
Edit the php.ini file and add the following two lines of configuration:
extension=pdo.so extension=pdo_mysql.so
5. Connect to MySQL database
Use PDO in PHP code to connect to MySQL database:
try { $pdo = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password'); echo '连接成功!'; } catch(PDOException $e) { echo '连接错误: ' . $e->getMessage(); }
The above is the source Detailed steps and code examples for code compilation, installation of PHP, enabling PDO extension and connecting to MySQL database. I hope this helps you, and I wish you success in setting up a web server!
The above is the detailed content of Source code compilation and installation of PHP PDO MySQL: detailed step analysis. For more information, please follow other related articles on the PHP Chinese website!