Home Sql Mysql-example sql php array
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

sql php array

Advertisement
SQL PHP Array support simple dimension array. The SQL PHP Array is used to populate array from MySQL database.

sql php array

     

SQL PHP Array support simple dimension array. The SQL PHP Array is used to populate array from MySQL database.

Understand with Example 

The Tutorial illustrate an example from 'SQL PHP Array'. To understand and grasp the example we include a PHP server side script that create an array that fetches a data records from table 'stu' of database 'komal'. The code returns only those records of value which are passed as id value in the function. 

 

 

sql_php_array.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
2ajay

Id Name
3santosh

Id Name
4rakesh
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 23, 2009

Related Tutorials

Ask Questions?    Discuss: sql php array   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com