How to fetch record from database in PHP

First we have to create database in phpmyadmin and then we have to create a table.
For example :
Database :          yourdbname
Table      :           user

id                           name                     email

1                               arvind                     1@email.com

2                               ajay                        2@email.com

3                               alok                        3@email.com

id = Primary Key

Here is the following code in php to fetch data from database and show it on output-

<?php

$connection = mysqli_connect(“database-hostname”,”database-username”,”database-password”,”database name”);

//Error checking

if (!$connection) {
die(‘Connection Error: ‘ . mysqli_connect_error());
}

$sql= “SELECT * FROM user WHERE 1”;
$qry= mysqli_query( $sql );

?>

<table>

<tr>
<td>ID </td> <td> studentname </td> <td> Email</td>
</tr>

<?php
while  ( $rs = mysql_fetch_array($qry))
{
?>
<tr>
<td> <?php echo $rs[id];</td> <td> <?php echo $rs[name]; ?></td> <td>  <?php echo $rs[email]; </td>
</tr>
<?php
}
?>
</table>

Leave a Reply

Your email address will not be published. Required fields are marked *