×

Please Login or Register to continue.

4
(1.7K Views)

The connection part is more important in PHP. I am trying to connect the HTML page to MySQL database using PHP PDO but getting errors to connect the database. I want to make a connection between the HTML page and the MYSQL database using PHP PDO. I am a beginner in PHP PDO. I am learning PHP PDO. I know that PHP PDO is best for oops concepts. I want to make a connection string in PHP PDO without errors. I am using PHPMYADMIN admin panel.I want to connect the PHPMyAdmin database table to an HTML page using PHP. 

(4.3K Points)
in PHP

Share

2 Answers
(9.4K Points)
(edited)
2

The PHP PDO provides a lightweight system to access databases. The full form of PDO is a PHP data object. You can connect the HTML page to the MySQL database easily by creating a simple connection file. First of all, if you are a beginner and you just started learning PHP and you want to connect database using PHP PDO then you have to follow some steps. If you know that how to connect an HTML page to a MySQL database using PHP MYSQLI then it will be very easy to understand. 
Let's understand these steps - 


How to connect database in PHP PDO? 


Install and configure local server - The local server works on your local computer. You can download Xampp or wamp any local server and install on your computer.
Create a PDO connection file - The connection file is known as the PDO config file which is used to connect the HTML page to the MySQL database using PHP PDO. 
Let's create PDO connection file - 
config.php 

<?php
define('DBNAME','test');
define('DBUSER','root');
define('DBPASS','');
define('DBHOST','localhost');
try {
  $dc = new PDO("mysql:host=".DBHOST.";dbname=".DBNAME, DBUSER, DBPASS);
  $dc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Your page is connected with database successfully..";
} catch(PDOException $e) {
  echo "Issue -> Connection failed: " . $e->getMessage();
}
//dc- Database Connection 
?>


In the PDO connection file above, we created four constants to store required connection credentials like - Server Host, Database username, Database Password, and Database name. To connect the database to the page using PHP PDO, you have to provide correct connection credentials or can say local or live server credentials. If given credentials are incorrect then you will get a connection failed exception with complete access denied details or if you provide correct details then you will get connected database successfully.  In PHP MySQL, we can connect only MySQL database but in PHP PDO, we can connect many databases like - 10+ (12 ) databases. 
Use this PDO config file in all pages to perform actions on Data like - create, reading, updating, and deleting(CRUD) using PHP PDO. 
Your PDO connection file is created and your database is connected successfully. 


Comment

(3.4K Points)
(edited)
1

The connection file is a base file to connect the database using PDO. PDO provides an easy way to create a connection between the page to the database. You can connect many databases using PHP PDO. 

Creating a connection using PDO :-

The PDO connection depends on server connection details. If your server details are correct then it will be connected successfully or if your server details are wrong then it will not be connected. You have to provide the correct host, username, database, and password. 

Let's create a PDO connection:-

<?php
ob_start(); session_start();
$databaseHost='localhost';
$databaseUser='root'; 
$databasePassword=''; 
$databaseName='test';
try {
  $conn = new PDO("mysql:host=".$databaseHost.";dbname=".$databaseName, $databaseUser, $databasePassword);
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  //echo 'PDO Connected Successfully'; 
} catch(PDOException $error) {
  //echo "Something went wrong " . $error->getMessage();
}
?>

Set server connection details in variables. Make sure all details are correct. 

Let's fetch and display data rows using PHP PDO and MySQL database. We know about fetch and display in HTML table using PHP MySQLi. Here, we will fetch and display data from the database in PHP PDO. 

First of all, create a database table -

Open your PHPMyAdmin and create a database. 

Let's say database name test 

Now, create a table inside the test database with the name products

CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `title` varchar(255) DEFAULT NULL,
  `price` double NOT NULL
);

Insert some values by query below.  

INSERT INTO `products` (`id`, `title`, `price`) VALUES
(1, 'Mobile Phone Xyz', 15000),
(2, 'Laptop 5i', 72000),
(3, 'Tablet 8 GB ', 26999);

We already created a connection between the HTML page to MySQL database using PHP PDO.

Save in config.php or you can save it as a pdo-config.php file. 

Create another PHP file index.php and include that pdo-config.php file at the top of the index page.  

First of all, select data rows from the database using the Select query. 

Use PDO connection variable to provide connection authority to page. 

Fetch all the data and display it in an HTML table using a while loop. 

index.php 

<?php
require_once('pdo-config.php'); 
 $stmt = $conn->query('SELECT * FROM products ORDER BY id DESC');
$rows=$stmt->fetchAll();
?>
    <table width='30%' border=1>
            <td>Product Name</td>
            <td>Product Price </td> 
        </tr>
        <?php 
        foreach($rows as $row) {         
            echo "<tr>";
            echo "<td>".$row['title']."</td>";
            echo "<td>".$row['price']."</td>"; 
                    
        }
        ?>
    </table>

We created a PDO connection successfully. 


Comment

Related Questions :-

Featured Items:-


Certificate system in PHP website | PHP scripts

$22



Home Credito loan finance management multipurpose system in PHP

$65



Result management system with Marksheet in PHP website | PHP Scripts

$39




Your Answer

You can post your answer after login..