PHP Code : How To Connect Php To MySQL Database Using PDO

PHP And MySQL Code : How To Connect Php And MySQL Database And Display Data.


________________________________________________________

In this Php Tutorial we will Learn How To Connect PHP5 With MySQL Database  And Show Database Table Data using PDO .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .


 


Php Source Code:


<?php

// php code -> 1bestcsharp.blogspot.com

// connect php to mysql database using PDO
// display data from mysql database

try {

    // trying to connect with mysqli database
    // put your hostname,your database name, user name (root by default)
    // and your password if you have one 

    $pdoConnect = new PDO("mysql:host=localhost;dbname=test_db","root","");
} catch (PDOException $exc) {

    // cath the onnection propleme

    echo $exc->getMessage();
    exit();
}

// result of the mysql select query


$pdoResult = $pdoConnect->query("SELECT * FROM users");

// displaying data from database mysql using foreach loop


foreach ($pdoResult as $row)
{
    echo "$row[0] - $row[1] - $row[2] - $row[3]<br>";
}



///////////////OUTPUT:



php connect to mysql and display data
Data From Database





Share this

Related Posts

Previous
Next Post »

1 comments:

comments
20 janvier 2020 à 22:53 delete

Thank you so much for this useful information. looking more from your side to update us on more updates and advancements

Reply
avatar