Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example PHP SQL Result

Related Tutorials


 
 

Share on Google+Share on Google+

PHP SQL Result

Advertisement
To understand how to display the result returned from the sql query execution, we have created sql_result.php page.

PHP SQL Result

     

This example illustrates how to display result on the browser and how to use the result in the php application.

To understand how to display the result returned  from the sql query execution, we have created sql_result.php page. First, connection from database is created using mysql_connect("hostName", "userName", "Password"), database is selected by mysql_select_db("databaseName", connectionObject). Now, query "SELECT * FROM users" is executed using mysql_query() method to select the data from the table "users" in the database. The result is stored in the " result" variable in php. The iteration can be performed using while statement as given below:

 

 

 

 

 

while($row = mysql_fetch_array($result)){
  echo $row['username'] . " " . $row['email'];
  echo "<br>";
}

Table: users

Source Code of sql_result.php 

<?php
  $con = mysql_connect("localhost","root","root");
  if (!$con){
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);

  $result = mysql_query("SELECT * FROM users");

  echo "<table border='1'>
  <tr>
  <th>User Name</th>
  <th>Email Address</th>
  </tr>";

  while($row = mysql_fetch_array($result)){
  echo "<tr>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "</tr>";
  }
  echo "</table>";

  mysql_close($con);
?>

Download Source Code

Output:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 20, 2009

Related Tutorials

Discuss: PHP SQL Result  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com