PHP SQL Where

PHP SQL Where is used to modify or change the structure of table definition based on the condition specified in Where Clause.

PHP SQL Where

PHP SQL Where

     

PHP SQL Where is used to modify or change the structure of  table definition based on the condition specified in Where Clause. The PHP SQL Where allows you to update the field based on the condition specified in Where Clause.

Understand with Example

The Tutorial illustrate an example from 'PHP SQL Where'. To grasp the example we create a emp_table whose required fields and datatypes are specified.  In the figure below the first figure show before update the table and the second figure show after update the table.

 

 

 

Table: emp_table before update

 

Table: emp_table after update

 

The PHP SQL Where starts with the <?php tag and end with ?>. The $ variable con is used to assigned the value of mysql_connect () function that accept the parameter local host, user and password to authenticate the connection between PHP and MySQL database. The die script executed when there is a connection failure between the PHP and MySQL. Once the connection is built successfully, the update query operation is performed to update the table 'emp_name' and set the new value for 'emp_name' to sandeep based on the 'emp_id = 1' condition specified in Where Clause. The While loop is used to execute the code repeatedly based on Boolean condition. The loop fetches the records from table until the condition evaluate to true.

Source Code of update_where.php 

<?php
  $con = mysql_connect("localhost""root""root");
  if (!$con){
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db("test", $con);

  $update = "UPDATE emp_table SET emp_name = 'sandeep' WHERE emp_id = 1";

  mysql_query($update, $con);

  echo "Column <b>emp_name</b> updated successfully<br><br>";

  $result = mysql_query("SELECT * FROM emp_table");

  while ($row = mysql_fetch_array($result)) {
  echo "<br>";
  echo $row['emp_id'] "<br>";
  echo $row['emp_name'] "<br>";
  echo $row['emp_designation'] "<br>";
  echo "<br>";
  }

  mysql_close($con);
?>

Download Source Code

Output: