Saturday, November 22, 2014

Php script to access database and display values

The first task we faced was to flex our muscles and access mysql database and to display the values in the form of a table.

We found out from google and wrote this program:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'cec123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT USN,student_name FROM students';

mysql_select_db('cec');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
    echo "USN :{$row['USN']}  <br> ".
         "Student Name {$row['student_name']} <br> ";
}
echo "Fetched data successfully!!!!!!!!!\n";
mysql_close($conn);
?>

The output of this program thrilled us. It was a start which we desperately needed.
In the browser
Here it was:
USN :1ce10cs001
Student Name Mukesh
USN :1ce10cs002
Student Name Suresh
USN :1ce10cs003
Student Name Ravi
USN :1ce11ec003
Student Name Satish
USN :1ce11ec002
Student Name Sunil
USN :1ce11ec001
Student Name Sowmya
Fetched data successfully!!!!!!!!!

No comments:

Post a Comment