Share on Google+Share on Google+

PHP SQL IF Statement

Advertisement
This example illustrates how to use the 'if' statement when accessing the data from mysql database table in php. 

PHP SQL IF Statement

 This example illustrates how to use the 'if' statement when accessing the data from mysql database table in php. 

In this example we create a connection from database table using mysql_connect("hostName", "userName", "Password"). After the connection created we choose database by mysql_select_db("databaseName", connectionObject) and finally we execute query by mysql_query("SELECT * FROM tableName"). The founded result display in the table as row and column wise. Here we are using the if statement, so only one data will display here as shown in the output.

 

Source Code of IF_Statement.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 emp");
  echo "<table border='1'>
    <tr>
      <th>id</th>
      <th>Name</th>
    </tr>";
    if($row = mysql_fetch_array($result)) {
      echo "<tr>";
      echo "<td>" . $row['id'] "</td>";
      echo "<td>" . $row['name'] "</td>";
      echo "</tr>";
    }
  echo "</table>";
  
  mysql_close($con);
?>

 

Output:

 

Download Source Code

Advertisements

Posted on: December 10, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.