Home >Backend Development >PHP Tutorial >Source code compilation and installation of PHP PDO MySQL: detailed step analysis

Source code compilation and installation of PHP PDO MySQL: detailed step analysis

WBOY
WBOYOriginal
2024-03-07 16:36:04978browse

源代码编译安装PHP PDO MySQL:详细步骤解析

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:

  1. GCC Compiler
  2. zlib library
  3. libxml2 library
  4. MySQL database

2. Download the source code package

  1. Download the latest Version of PHP source code package:

    wget https://www.php.net/distributions/php-8.1.3.tar.gz
  2. Download the latest version of PDO MySQL driver source code package:

    wget https://pecl.php.net/get/PDO_MYSQL

3. Compile and install PHP

  1. Decompress the PHP source code package:

    tar -zxvf php-8.1.3.tar.gz
  2. 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

  1. Extract the downloaded PDO MySQL driver source code package:

    tar -zxvf PDO_MYSQL
  2. 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
  3. 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

  1. 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!

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