Hi Friends,
Hope u r doing well.
In this post u r going to learn "How to fetch data from Mysql Database".
If u r not aware of "How to store data in Mysql Database" u better go through My Previous Tutorial .
The Data fetching process can be divided in to few steps.
1. Connecting to Database.
2. Fetching the Data as resource.
3. Extracting data from resource.
Connecting to Database:
I explained this concept clearly in my previous post .
Fetching data from Database:
This can be done through mysql_query() function.
Syntax: mysql_query(select_query, connection_object);
example: $result=mysql_query("select * from user_table",$connection);
But the best practice is to prepare query first and then execute it.
for example:
<?php
$sql_select="select * from user_table";
$result= mysql_query($sql_select,$connection);
?>
Here, $result is a resource. A resource is a special variable that holds the reference of another external resource which contains actual fetched data.
Extracting Data from Resource:
The data from a resource can be fetched using a mysql_fetch_array() function
syntax: mysql_fetch_array(result_resource, result_type);
in the above syntax the second parameter result_type is optional one.
example: mysql_fetch_array($result, MYSQL_BOTH)
Generally this function is used with in a while loop, which ends when all rows fetched from database are extracted. This function returns an array which contains one record filed's names and their corresponding values as key value pairs.
Let us assume we have two records in table 'user_table' with fields uname, age, profession.
like :
-------------------------------------------------------------
uname age profession
Venkatesh 25 Web Developer
Mahesh 26 Marketing Executive
-------------------------------------------------------------
Those two rows are fetched from Database using mysql_query() function.
We can extract those two rows as arrays as follows:
<?php
while($row=mysql_fetch_array($result))
{
echo $row['uname']." ".$row['age']." ".$row['profession'];
echo "<br>"; // moving cursor to next line using html break tag.
}?>
The output will be:
Venkatesh 25 Web Developer
Mahesh 26 Marketing Executive
The while loop will get rotated till all rows are extracted.
So, We can process the fetched data as per our requirement.
Here, in this example am going to print them in tabular format.
Program:
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('my_db',$con);
$sql_select="select * from user_table";
$result=mysql_query($sql_select,$con);
//from here am embedding html code in php.
?>
<table border="1">
<tr><th>Name</th><th>Age</th><th>Profession</th></tr>
<?php
while($row=mysql_fetch_array($result))
{
//while loop starts here
?>
<tr><td><?=$row['uname'];?></td>
<td><?=$row['age'];?></td>
<td><?=$row['profession'];?></td>
</tr>
<?php
//while loop ends here
}
?>
</table>
Output:
I hope you understood well.
In my next post I will explain you how to upload a file in PHP.
Up to then Take Care Bye..
Urs Venky ... :)
Hope u r doing well.
In this post u r going to learn "How to fetch data from Mysql Database".
If u r not aware of "How to store data in Mysql Database" u better go through My Previous Tutorial .
The Data fetching process can be divided in to few steps.
1. Connecting to Database.
2. Fetching the Data as resource.
3. Extracting data from resource.
Connecting to Database:
I explained this concept clearly in my previous post .
Fetching data from Database:
This can be done through mysql_query() function.
Syntax: mysql_query(select_query, connection_object);
example: $result=mysql_query("select * from user_table",$connection);
But the best practice is to prepare query first and then execute it.
for example:
<?php
$sql_select="select * from user_table";
$result= mysql_query($sql_select,$connection);
?>
Here, $result is a resource. A resource is a special variable that holds the reference of another external resource which contains actual fetched data.
Extracting Data from Resource:
The data from a resource can be fetched using a mysql_fetch_array() function
syntax: mysql_fetch_array(result_resource, result_type);
in the above syntax the second parameter result_type is optional one.
example: mysql_fetch_array($result, MYSQL_BOTH)
Generally this function is used with in a while loop, which ends when all rows fetched from database are extracted. This function returns an array which contains one record filed's names and their corresponding values as key value pairs.
Let us assume we have two records in table 'user_table' with fields uname, age, profession.
like :
-------------------------------------------------------------
uname age profession
Venkatesh 25 Web Developer
Mahesh 26 Marketing Executive
-------------------------------------------------------------
Those two rows are fetched from Database using mysql_query() function.
We can extract those two rows as arrays as follows:
<?php
while($row=mysql_fetch_array($result))
{
echo $row['uname']." ".$row['age']." ".$row['profession'];
echo "<br>"; // moving cursor to next line using html break tag.
}?>
The output will be:
Venkatesh 25 Web Developer
Mahesh 26 Marketing Executive
The while loop will get rotated till all rows are extracted.
So, We can process the fetched data as per our requirement.
Here, in this example am going to print them in tabular format.
Program:
<?php
$con=mysql_connect('localhost','root','');
mysql_select_db('my_db',$con);
$sql_select="select * from user_table";
$result=mysql_query($sql_select,$con);
//from here am embedding html code in php.
?>
<table border="1">
<tr><th>Name</th><th>Age</th><th>Profession</th></tr>
<?php
while($row=mysql_fetch_array($result))
{
//while loop starts here
?>
<tr><td><?=$row['uname'];?></td>
<td><?=$row['age'];?></td>
<td><?=$row['profession'];?></td>
</tr>
<?php
//while loop ends here
}
?>
</table>
Output:
I hope you understood well.
In my next post I will explain you how to upload a file in PHP.
Up to then Take Care Bye..
Urs Venky ... :)