Share on Google+Share on Google+

SQL PHP

Advertisement
PHP is a server side scripting language. To access the data in PHP,you must create a connection to the database.

SQL PHP

     

PHP is a server side scripting language. To access the data in PHP,you must create a connection to the database. The PHP server side script include the host, user, password and database that is used to connect the MySQL database. The connection is done  with the help of mysql_connect (  ) function. Once your connection is built, you can use return the records from table 'stu' in database using select query. The echo print all the records values of specified field from table 'stu' in your browser.

sql_php.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());
$sql="Select * from stu ";
$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
2ajay
3santosh
4rakesh
 

Advertisements

Advertisement

Posted on: January 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Comments:0