Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Show Record From database using PHP and MYSQL using Table Format:-

We will use the select queries to select a record from the database.

select * from tablename;

PHP provides three different functions to display a record from the database.

1)  mysqli_fetch_array():-  It is used to show record using associative array and index array pattern.

while($var = mysqli_fetch_array($result)
{

   echo $var['columnname'];  // we can write column name also to display data 
   echo $var[index]   // we can write index also where first column index start from 0

}


We always use the while loop to display a record from the database and the while loop will be true until condition false.

Example with complete PHP Code-

<?php

$conn = mysqli_connect('localhost',"root","","php12batch");
$res = mysqli_query($conn,"select * from student");

while($x = mysqli_fetch_array($res))
{

echo $x[0]. " ".$x[1]." ".$x[2]." ".$x[3]. " ".$x[4];
}






?>

Example with complete PHP Code-

<?php

$conn = mysqli_connect('localhost',"root","","php12batch");
$res = mysqli_query($conn,"select * from student");

while($x = mysqli_fetch_array($res))
{

echo $x['rno']. " ".$x['name']." ".$x['branch']." ".$x['fees']. " ".$x['mobile'];
}






?>



2) mysqli_fetch_row():-  It is used to show record using index array

It is used to display a single row using an index array pattern.

We can display data without using Loop.

$var = mysqli_fetch_row($result);

echo $var[0];  //0 is the index

Example with complete PHP Code-

<?php

$conn = mysqli_connect('localhost',"root","","php12batch");
$res = mysqli_query($conn,"select * from student");

while($x = mysqli_fetch_row($res))
{

echo $x[0]. " ".$x[1]." ".$x[2]." ".$x[3]. " ".$x[4];
}






?>


3) mysqli_fetch_assoc():-  It is used to display data using an associative array.


while($var = mysqli_fetch_assoc($result)
{

   echo $var['columnname'];  // we can write column name also to display data 
   echo $var[index]   // we can write index also where first column index start from 0

}
Example with complete PHP Code-

<?php

$conn = mysqli_connect('localhost',"root","","php12batch");
$res = mysqli_query($conn,"select * from student");

while($x = mysqli_fetch_assoc($res))
{

echo $x['rno']. " ".$x['name']." ".$x['branch']." ".$x['fees']. " ".$x['mobile'];
}






?>

4) mysqli_fetch_object():-  It is used to display data using object pattern;

while($var = mysqli_fetch_object($result)
{

   echo $var->columnname;  // we can write column name also to display data 
  

}

Example with complete PHP Code-

<?php

$conn = mysqli_connect('localhost',"root","","php12batch");
$res = mysqli_query($conn,"select * from student");

while($x = mysqli_fetch_object($res))
{

echo $x->rno. " ".$x->name." ".$x->branch." ".$x->fees. " ".$x->mobile;
}






?>



Complete Code Of PHP to show record in Table Format

<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
table
{
width:600px;
}
table th
{
background-color:yellow;
color:blue;
font-size: 25px;
}
        table td
{
background-color:orange;
color:black;
font-size: 20px;
text-align: center;
}
</style>
</head>
<body>
<h1>View Student Record</h1>
<hr>
<table border="1">
<tr><th>Rno</th><th>Sname</th><th>Branch</th><th>Fees</th></tr>
<?php
$conn= mysqli_connect('localhost','root','','php12batch');
$res = mysqli_query($conn,"select * from student");
while($x = mysqli_fetch_array($res))
{
  echo "<tr><td>".$x[0],'</td><td>' ,$x[1],'</td><td>',$x[2],'</td><td> ',$x[3],"</td></tr>";

}
?>
</table>
</body>
</html>



Post a Comment

POST Answer of Questions and ASK to Doubt

Previous Post Next Post