Friday, 26 June 2015

PHP Select Data From MySQL

In this tutorial i will show you that how to select data from mysql using php , this tutorial will use PHP and MySlql together to select data from php mysql database by selecting the data from mysql database you can display the selected data on the webpage here is the steps.


Here i use the previously created  data insert into mysql  table to select the data from MySQL and the table is as follow

1.Step
sample database design for table users
contains user details first name , last name and city

CREATE TABLE `users` (
`user_id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`first_name` VARCHAR( 25 ) NOT NULL ,
`last_name` VARCHAR( 25 ) NOT NULL ,
`city_name` VARCHAR( 30 ) NOT NULL
) ENGINE = MYISAM 

2.Step following method select the user data by using mysql_fetch_row() function
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);

while($row=mysql_fetch_row($result_set))
{
   ?>
   <tr>
   <td><?php echo $row[1]; ?></td>
   <td><?php echo $row[2]; ?></td>
   <td><?php echo $row[3]; ?></td>
   </tr>
   <?php
}

full script
index.php
<?php
mysql_connect("localhost","root");
mysql_select_db("dbtuts");
$sql_query="SELECT * FROM users";
$result_set=mysql_query($sql_query);
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>PHP Select Data From MySql - By Cleartuts</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<center>
<div id="header">
 <div id="content">
        <label>PHP Select Data From MySql - By Cleartuts</label>
        </div>
</div>
<div id="body">
 <div id="content">
        <table align="center">
        <tr>
        <th>First Name</th>
        <th>Last Name</th>
        <th>City</th>
        </tr>
        <?php
 if(mysql_num_rows($result_set)>0)
 {
            while($row=mysql_fetch_row($result_set))
            {
         ?>
                <tr>
                <td><?php echo $row[1]; ?></td>
                <td><?php echo $row[2]; ?></td>
                <td><?php echo $row[3]; ?></td>
                </tr>
                <?php
            }
 }
 else
 {
            ?>
            <tr>
            <th colspan="3">There's No data found !!!</th>
            </tr>
            <?php
 }
 ?>
    </table>
    </div>
</div>

<div id="footer">
 <div id="content">
        <hr /><br/>
        <label>for more tutorials and blog tips visit <a href="http://cleartuts.blogspot.com"> : cleartuts.com</a></label>
        </div>
</div>
</center>
</body>
</html>

style.css
@charset "utf-8";
/* CSS Document */
*
{
 margin:0;
 padding:0;
}
body
{
 background:#f9f9f9;
 font-family:"Courier New", Courier, monospace;
}
#header
{
 width:100%;
 height:50px;
 background:#00a2d1;
 color:#f9f9f9;
 font-family:"Lucida Sans Unicode", "Lucida Grande", sans-serif;
 font-size:35px;
 text-align:center;
}
#body
{
 margin-top:80px;
}
table
{
 width:40%;
 font-family:Tahoma, Geneva, sans-serif;
 font-weight:bolder;
 color:#999;
}
table,td,th
{
 border-collapse:collapse;
 border:solid #d0d0d0 1px;
 padding:20px;
}
#footer
{
 margin-top:100px;
 font-weight:bolder;
 color:#00a2d1;
 font-family:Verdana, Geneva, sans-serif;
 font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
 font-family:"Courier New", Courier, monospace;
 font-size:20px;
}
#footer a
{
 color:#004567;
 text-decoration:none;
}
#footer a:hover
{
 color:brown;
}
#footer hr
{
 width:80%;
}

0 comments:

Post a Comment