connect PHP with database

<!DOCTYPE html>
<html lang=”en-US”>
<head>
<meta charset=”UTF-8″>
<title>fetch.php</title>
<style type = “text/css”>
table, th, td {border: 1px solid black};
</style>
</head>
<body>
<?php

try{
$connect=new PDO(‘mysql:hostname=localhost;dbname=learn’,”root”);
$connect->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$query=’SELECT * FROM CONTACT’;
print “<table> \n”;
$field=$connect->query($query);
$row=$field->fetch(PDO::FETCH_ASSOC);
print “<tr> \n”;
foreach ($row as $field=>$value){
print “<th> $field </th> \n”;
}
print “</tr> \n”;
$data=$connect->query($query);
$data->setFetchMode(PDO::FETCH_ASSOC);
foreach($data as $row){
print “<tr> \n”;
foreach($row as $name=>$value){
print “<td> $value </td> \n”;
}
print “</tr> \n”;
}
print “</table> \n”;
}
catch(PDOException $e) {
echo ‘ERROR: ‘ . $e->getMessage();
}
?>
</body>
</html>

Continue reading “connect PHP with database”

Advertisements