Databases| SQL| MySQL| Questions? | Software Development
 

PHP SQL IF Statement

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

                         

» View all related tutorials
Related Tags: sql mysql c string com function fun io sed const char int ai padding length character case cas if ie

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
 
Tell A Friend
Your Friend Name

 

 
Recently Viewed
Software Solutions
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.