How to select data from mysql database using PHP
In this tutorial we will learn how to select data from mysql database.First of all we need to connect to the database from which we will collect data.
$con=mysqli_connect("localhost","root","","youtube");
In the above code the root is the username of the mysql and the database name is "youtube".You can replace your database name in this portion.Now we will select data from database using select sql.
$query="select * from registration";
Here the table name is registration,the structure of the table is as follows:-
Column Name | Type |
---|---|
rid | int(11) |
name | varchar(100) |
fathersname | varchar(100) |
mothersname | varchar(100) |
address | varchar(200) |
varchar(200) | |
uname | varchar(200),Primary Key |
password | varchar(200) |
In the next step we will execute the query ,after execution we will get a result set.Now we will supply that result set to also mysqli_fetch_row(),which will fetch the rows from the the result set. we need a loop in the mysqli_fetch_row() so that we get all the rows from the database.
Lets check the code :
<?php
$con=mysqli_connect("localhost","root","","youtube");
$query="select * from registration";
$sql=mysqli_query($con,$query);
?>
<table border="0" align="center" >
<thead>
<th>Registration Id</th>
<th>Station name</th>
<th>Station name</th>
<th>Station name</th>
<th>Station name</th>
<th>Station name</th>
</thead>
<tbody>
<?php
while($rs1=mysqli_fetch_row($sql))
{
echo "<tr>";
echo "<td>echo $rs1[0]</td>";
echo "<td>echo $rs1[1]</td>";
echo "<td>echo $rs1[2]</td>";
echo "<td>echo $rs1[3]</td>";
echo "<td>echo $rs1[4]</td>";
echo "<td>echo $rs1[5]</td>";
echo "</tr>";
}
?>
</tbody>
</table>
In this code i am using a table to show the data .Enjoy coding.#sbphptricks
Comments
Post a Comment