Monday, 10 June 2013

How to read a record from mysql database in php - Where clause


Getting a data from mysql database is again very simple task in php.
You have to follow below steps -

  • Connect to MYSQL DB
  • Execute any select query ....This will return the record set
  • Use while loop to iterate through each record 



<?php
$mysqlcon=mysqli_connect("localhost","uid","password","emp_db");

// connect to mysql database using userid and password

if (mysqli_connect_errno())
  {
  echo "Error while connecting to MySQL: " . mysqli_connect_error();
  }

//***********Connection Successful to emp_db database  in mysql************


// Now you can add a record in mysql database

$dbresult = mysqli_query($mysqlcon,"SELECT * FROM emp_db
WHERE age=20");

//dbresult contains the recordset....Now read each record from that set.

while($myrecord = mysqli_fetch_array($dbresult ))
  {
  echo $
myrecord ['name'] ;
  echo "<br>";
  }

?>

This is how we can read values from a mysql database table.

No comments:

Post a Comment