PHP SQL Result

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

PHP SQL Result

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: