Hello friends, welcome again, this is my 11th tutorial of PHP and Today I am sharing the concept of using database in PHP. Database is a vital portion of a programming language because this is where we store our data so it's really important that we have a clear idea about how to connect to database, how to fetch data from database, storing data to database and updating and deleting data from database.
In PHP we can connect to database by using five simple steps as demonstrated below :
 <?php  
//Step-1 : Create a database connection
$connection=mysql_connect("localhost","root","root");
if(!$connection) {
 die("Database Connection error" . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db("widget_corp",$connection);
if(!$db) {
  die("Database Selection error" . mysql_error());
}
//Step 3 : Perform database Queury
$result=mysql_query("select * from subjects",$connection);
if(!$result) {
 die("No rows Fetch" . mysql_error());
}

//Step 4 : Use returned data
while($row=mysql_fetch_array($result)) {
 //echo $row[1]." ".$row[2];
 echo $row["menu_name"]." ".$row["position"];
}

//Step 5 : Close Connection 
mysql_close($connection);
?>
In the above code I have demonstrated 5 simple steps for connecting to database. Hope it will be useful for you. Feel free to post your ideas in the comments below.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.