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>";
}
// 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:
1 comments:
commentsThank you so much for this useful information. looking more from your side to update us on more updates and advancements
Reply