Sunday, October 7, 2012

Retrieving mysql data into php web page


Hai friends today i am disusing about retrieving MySQL data into php web form for this first we have to follow the below steps.1. First of all open phpmyadmin and then create a new database as student then create a new table.
Creating table iscompletelydepends on you
but i am discussing about a table that contains student’s information.



After open this page select the database thats shows leftside of the above figure.then click on SQL query shown below.



then copy paste the following queries and then press go button then the following queries get executed
create table student (rollno int primary key,sname varchar(30),class varchar(30),averageMarks int);
insert into student (rollno,sname,class,averageMarks) values(1,”poorna”,”MCA”,75);
insert into student (rollno,sname,class,averageMarks) values(2,”mohan”,”MCA”,78);
insert into student (rollno,sname,class,averageMarks) values(3,”Ramakrishna”,”MCA”,76);
insert into student (rollno,sname,class,averageMarks) values(4,”kalyan”,”MCA”,89);
insert into student (rollno,sname,class,averageMarks) values(5,”naidu”,”MCA”,79);
insert into student (rollno,sname,class,averageMarks) values(6,”praveen”,”MCA”,74);
after you press go button then student table has been created and values also been inserted in it
make sure that the table name student is not present in your database if present change the table name and try it
if you have a predefined database with table already then skip this step and proceed further.
2.Now create a php page and add the following code that show below
<?php>
/*first of all connect to database through php by using following code here root means user name it will changes if you already have a different username or by default MySQL user is root and after that we have to mention password by default there is no password for MySQL if you create a password then please mention here after that select your database here i am using my database as students yours should changes*/

$db1=mysql_connect("localhost", "root", "") or die(mysql_error()); 
$selectdb=mysql_select_db("student", $db1) or die(mysql_error());

/*now retrieving data from database*/

echo "<table border=1 cellspacing=0 align=center>";
echo "<tr><td>RollNumber</td><td>Student Name</td><td>Student Class</td><td>Average Marks</td></tr>";
$qr1=mysql_query("select * from student");
while($mr1=mysql_fetch_array($qr1))
{
$rollnumber=$mr1['rollno'];     //here we mention database element name in $mr1
$name=$mr1['sname'];           //$name is a variable it should be our choice to choose the name
$class=$mr1['class'];
$averagemarks=$mr1['averageMarks'];
echo '<tr><td>'.$rollnumber.'</td><td>'.$name.'</td><td>'.$class.'</td><td>'.$averagemarks.'</td></tr>';
}
echo"</table>";
?>
This is the php code to retrieve data from MySQL
3.now run the php page from localhost the the data you entered in the mysql page will be available in your webpage

No comments:

Post a Comment