Home Sql Mysql-example PHP SQL IF Statement



PHP SQL IF Statement
Posted on: January 17, 2009 at 12:00 AM
This example illustrates how to use the 'if' statement in php when accessing the data from table in mysql database.

PHP SQL IF Statement

     

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

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"). mysql_fetch_array() returns an array corresponding to the fetched row. 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

Related Tags for PHP SQL IF Statement:
phpsqlmysqlcdatabasetabledatastatethistabifexamplestatementtobaseexamssieilusefromceinasstamnttresemasemehphowrateratesxawhenxampsessatmyisllmplstatccstrssthstaccessabablpleplomo


More Tutorials from this section

Ask Questions?    Discuss: PHP SQL IF Statement  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.