Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql Mysql-example PHP SQL Connection

Related Tutorials


 
 

Share on Google+Share on Google+

PHP SQL Connection

Advertisement
PHP SQL Connection is used to access and return the records from PHP and MySQL database and vice-versa.

PHP SQL Connection

     

PHP SQL Connection is used to access and return the records from PHP and MySQL database and vice-versa.

Understand with Example

The Tutorial illustrate an example that helps you to understand how to establish the database connection between PHP and SQL. To understand the example we create a table 'Stu' with the required fieldnames and datatypes respectively. The table 'Stu' has a Primary Key id.

 

 

 

 

Create table Stu:

CREATE TABLE `stu` (                     
          `id` int(11) NOT NULL ,  
          `name` varbinary(10) default NULL,     
          `class` int(11) default NULL,          
          PRIMARY KEY  (`id`)                    
        );

Insert data into Stu:

The insert into keyword adds the records or rows values to the table 'Stu'.

INSERT INTO `stu` VALUES (1,'Ajay',12);
INSERT INTO `stu` VALUES (2,'Bhanu',12);
INSERT INTO `stu` VALUES (3,'Komal',12);
INSERT INTO `stu` VALUES (4,'Rakesh',12);
INSERT INTO `stu` VALUES (5,'Santosh',12);
INSERT INTO `stu` VALUES (6,'Tanuj',12);

php_sql_fetch.php:

To access the data from table 'stu' firstly we need to include the host, user, password and database that connects the PHP server side to MySQL database in the PHP server side scripts. The variable Connection accept the host , user and password. Once the  connection is built successfully, you are able to access and return the records from table 'stu'.

<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> 
		<td><b>Class</b></td>";
echo "</tr>";
while($sql_row=mysql_fetch_array($sql_result))
{
	$id=$sql_row["id"];
	$name=$sql_row["name"];
	$class=$sql_row["class"];
	echo "<tr><td>".$id."</td>";
	echo "<td>".$name."</td>";
	echo "<td>".$class."</td></tr>";
}	
echo "</table>"
?>
</body>
</html>

Result

Id Name Class
1 Ajay 12
2 Bhanu 12
3 Komal 12
4 Rakesh 12
5 Santosh 12
6 Tanuj 12
Advertisement

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

Discuss: PHP SQL Connection  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
kumar gajurel
April 11, 2011
great articles

Its great tutor for us.......
sunil
September 13, 2011
php

how to run php program, I'm install wampserver but there are sum error to execute it. give me solution and second any other software to run the php program, if yes to give me name as well as link to download.
jpFaisal
February 15, 2012
ALso TESTED

What a fanstastic code. RUNS for me with SMOOTH RESULTS. Thanks GREAT. FAISAL.
maya
August 20, 2011
mysql connection to php

tell me connection are create php to mysql database
DMCA.com