PHP And MySQL : PHP ( PDO ) Using Foreach Loop Fetch All Data From MySQL Database

PHP PDO Tutorial: Display MySQL Database Records Using PDO In PHP

_______________________________________________________


php mysql foreach



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>';
}

?>



Share this

Related Posts

Previous
Next Post »

2 comments

comments