Last updated on November 11th, 2024 at 02:31 pm

PEAR stands for PHP Extension and Application Repository. This PHP script uses the PEAR DB library to connect to a MySQL database, execute a query, and retrieve results.

Create a Database of your choice and run these sql queries in the database.

CREATE TABLE mydb_test (
  mydb_test_id mediumint(9) NOT NULL auto_increment,
  mydb_test_stamp bigint(20),
  mydb_test_text varchar(50),
  PRIMARY KEY (mydb_test_id)
);
INSERT INTO mydb_test VALUES (1,783173,'My Data For Today');
INSERT INTO mydb_test VALUES (2,731637,'My Data For Tmrw');
INSERT INTO mydb_test VALUES (3,873612,'Day After');

Now our PHP file for connecting to the database and displaying the data from the db using PEAR.

Install db module using https://pear.php.net/package/DB

pear install DB

Here is the complete code.

<?php
require_once('DB.php');  // Load the PEAR DB library

$dbhost = 'localhost';
$dbuser = '<YOUR_DB_USERNAME>';
$dbpass = '<your_db_password>';
$dbname = 'YOUR_DB_NAME';

// Connect to the MySQL database using PEAR DB
$db = DB::connect("mysql://$dbuser:$dbpass@$dbhost/$dbname");

// Check if the connection was successful
if (PEAR::isError($db)) {
    die($db->getMessage());  // Terminate and display the error message if connection fails
}

$sql = 'SELECT * FROM mydb_test';  // SQL query to fetch all records from the 'mydb_test' table

$demoResult = $db->query($sql);  // Execute the query

// Loop through each row in the result set
while ($demoRow = $demoResult->fetchRow()) {
    echo $demoRow[2] . '<br />';  // Display the value of the third column in each row
}

$db->disconnect();  // Close the database connection
?>

PEAR DB Library:

  • require_once('DB.php'); includes the PEAR DB library, which is a database abstraction layer. It simplifies the connection and querying process and supports multiple database types (e.g., MySQL, PostgreSQL, Oracle).

Database Connection:

  • $db = DB::connect("mysql://$dbuser:$dbpass@$dbhost/$dbname"); creates a connection string for MySQL using the specified username, password, host, and database name.
  • Note: Replace <YOUR_DB_USERNAME>, <your_db_password>, and YOUR_DB_NAME with your actual database credentials.

Error Handling:

  • PEAR::isError($db): Checks if the connection failed, and die($db->getMessage()); terminates the script with an error message if it did.

Executing the Query:

  • $sql = 'SELECT * FROM mydb_test'; is the SQL query to retrieve all rows from the mydb_test table.
  • $demoResult = $db->query($sql); executes the query, and the results are stored in $demoResult.

Fetching Results:

  • while ($demoRow = $demoResult->fetchRow()): Loops through each row in the result set. fetchRow() returns each row as an array.
  • echo $demoRow[2] . '<br />'; displays the value in the third column of each row.

Closing the Connection:

  • $db->disconnect(); closes the connection to the database.

Note: PEAR DB is an older library. PDO (PHP Data Objects) is now preferred for new projects due to better support and features. However, if you are working in an environment with PEAR DB, this code will work as expected.