<?php
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'Manas9';
/*** mysql password ***/
$password = 'Manas9237';
//$mysqli = new mysqli("Manas9", "Manas9", "Manas9237", "test");
//$mysqli = new mysqli("localhost", "my_user", "my_password",
"world");
try {
//$dbh = new PDO("mysql:host=$hostname;dbname=animals", $username,
$password);
$dbconn= new PDO("mysql:host=$hostname;dbname=test", $username,
$password);
/*** echo a message saying we have connected ***/
echo 'Connected to database<br />';
/*** The SQL SELECT statement ***/
$sql = "SELECT * FROM emp1 ORDER BY fname";
/*** fetch into an PDOStatement object ***/
$stmt = $dbconn->query($sql);
/*** echo number of columns ***/
$result = $stmt->fetch(PDO::FETCH_NUM);
// $result = $stmt->fetch(PDO::FETCH_ASSOC);
/*** loop over the object directly ***/
foreach($result as $key=>$val)
{
echo $key.' - '.$val.'<br />';
}
/*** close the database connection ***/
$dbconn= null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
|