PHP PDO Tutorial: Display MySQL Database Records Using PDO In PHP
In this Php Tutorial we will see How To Fetch All Data From MySQL Database Table
In Php Using PDO .
I Use In This Tutorial:
- NetBeans IDE .
- XAMPP .
- PhpMyAdmin .
-MySQL Database .
*Course : Learn Php And Buid Cms Project Course
Php Source Code:
<!--Using Php Foreach Loop With MySQL Databse-->
<?php
$dsn = 'mysql:hosy=localhost;dbname=test_db';
$username = 'root';
$password = '';
try{
// connect to mysql
$con = new PDO($dsn,$username,$password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $ex) {
echo 'Not Connected '.$ex->getMessage();
}
// mysql select query
$stmt = $con->prepare('SELECT * FROM users');
$stmt->execute();
$users = $stmt->fetchAll();
foreach ($users as $user)
{
echo $user['id'].' - '.$user['fname'].' - '.$user['lname'].' - '.$user['age'].'<br>';
}
?>
2 comments
commentsHow do I make it look a little more fancy?
ReplyHello
Reply