PHP Examples - Databases - Mysql



PHP Examples 
 Databases - MySQL

  • Connect to database
mysql_connect("hostname","user","password");
  • Create database
mysql_query("CREATE DATABASE DB_name", connection);
  • Create table
mysql_query(" CREATE TABLE Table_name ( Name varchar(20),...)", connection);


...

Program:


<html >
<head>

<title>PHP Example 7 : DATABASES (MySQL)</title>
</head>

<body>
<?php
 
 echo "PHP Example 7";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"DATABASES: ( MySQL )";
 echo"</h1>";
 echo"<br/>";

 
 echo"<br>CONNECTING...";
 $con = mysql_connect("localhost","root","");  // Connecting to the database
 if(!$con)
   die('Connection Failed'.mysql_error());
 echo"<br>CONNECTED...";
 if(!mysql_select_db("DB1",$con)) 
 {
  if(mysql_query("CREATE DATABASE DB1",$con)) // Creating database
   echo"<br><br>DB1 Created...";
  else
   echo 'Error Occured...'.mysql_error();
  mysql_select_db("DB1",$con);  // Creating tables
 }

 mysql_query(" CREATE TABLE Students(
  Rollnum int NOT NULL,
  Name varchar(20),
  Mark int,
  PRIMARY KEY(Rollnum) )",$con);
  
 echo"<br><br>Table  Students Created...";  
   // Inserting data
 mysql_query(" INSERT INTO Students VALUES ('1','NIDHEESH','91')",$con);
 mysql_query(" INSERT INTO Students VALUES ('2','SAJITH','96')",$con);
 mysql_query(" INSERT INTO Students VALUES ('3','IRSHAD','88')",$con);
 mysql_query(" INSERT INTO Students VALUES ('4','AJISH','90')",$con);
 mysql_query(" INSERT INTO Students VALUES ('5','BINDU','90')",$con);
 mysql_query(" INSERT INTO Students VALUES ('6','RAFEEQ','95')",$con);
 echo"<br><br>Records added...";  
   
 $data= mysql_query(" SELECT * FROM Students ",$con); // Retreiving data

 echo "<table border='2'>
   <tr> 
   <th> Roll No</th>
   <th> Name</th>
   <th> Mark </th>
   </tr>";

 while($rec=mysql_fetch_array($data))
 {
  echo "<tr><td>".$rec['Rollnum']."</td><td>".$rec['Name']."</td><td>".$rec['Mark']."</td></tr>";  
 }
 echo"</table>";
 
 // mysql_query(" DROP DB1 ",$con);
 mysql_close($con);  // Closing connection

 echo "<br/><br/><br/><br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>

1 comment:

  1. Thanks for sharing this with us......this is really appreciating. PLease go to site picbear

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...