SQL PHP Where

SQL PHP Where is used to built the connection between the SQL and PHP.

SQL PHP Where

SQL PHP Where

     

SQL PHP Where is used to built the connection between the SQL and PHP. The PHP sever side script is used to retrieve the records that meet a specific criteria from database 

Understand with Example

The Tutorial illustrate an example from 'SQL PHP Where'. To understand and illustrate the example we use PHP server side script that include host, user, password and database used to connect to Mysql database. Once your connection is built, the php query include the Mysql query function and return the records that meet a specific criteria from table 'stu' in database 'komal'.

 

 

 

sql_php_where.php

<html>
<body>
<?php
$host = "localhost";
$user = "root";
$password = "root";
$database = "komal";
$connection = mysql_connect($host,$user,$password) 
	or die("Could not connect: ".mysql_error());
mysql_select_db($database,$connection) 
	or die("Error in selecting the database:".mysql_error());
$array = array(1,2,3,4);
foreach ($array as $value){
	$sql="Select * from stu where id =".$value;
	$sql_result=mysql_query($sql,$connection) 
		or exit("Sql Error".mysql_error());
	$sql_num=mysql_num_rows($sql_result);
	echo "<table width =\"20%\" bgcolor=\"#F5F5FF\">";
	echo "<tr>";
	echo "<td ><b>Id</b></td> <td><b>Name</b></td>";
	echo "</tr>";
	while($sql_row=mysql_fetch_array($sql_result)) {
		$id=$sql_row["id"];
		$name=$sql_row["name"];
		echo "<tr><td>".$id."</td>";
		echo "<td>".$name."</td>";
	}	
	echo "</table><br>";
}
?>
</body>
</html>

Output

Id Name
1 komal

 

Id Name
2 ajay

 

Id Name
3 santosh

 

Id Name
4 rakesh