SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
PHP Tutorials By Vineet Kumar Saini

                       Database Connectivity in PHP

This article explains how to insert and select data from a database into a HTML table in PHP.
Using an insert statement you can insert data into a database. Using a select statement you
can select the entire data from a database into a HTML table.

First of all we will create a database.

Create The Database

<html>
<body bgcolor="pink">
<center>
<?php
$con = mysql_connect ("localhost","root","");
$db="MCN";
if (!$con)
  {
  die ('Could not connect: ' . mysql_error());
  }
if (mysql_query ("CREATE DATABASE $db",$con))
  {
  echo "Your Database Created Which Name is : $db";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }
mysql_close ($con);
?>
</center>
}</body>
</html>

Output

In the above code, we created a database named MCN. Now we will create a table in the
MCN database.




                             www.vineetsaini.wordpress.com
PHP Tutorials By Vineet Kumar Saini




Create The Table

<html>
<body bgcolor="pink">
<center>
<?php
$con = mysql_connect ("localhost","root","");
if (!$con)
  {
  die ('Could not connect: ' . mysql_error());
  }
mysql_select_db ("MCN", $con);
$sql = "CREATE TABLE EMPLOYEE
(
Name varchar(50),
Designation varchar(50),
Sal int,
Qualification varchar(50)
)";
mysql_query($sql,$con);
echo "Your table created which is as follows";
mysql_close($con);
?>
<table bgcolor="skyblue" border="2">
<tr>
<td colspan=2 align="center">Employee Table</td>
</tr>
<td>Name</td><td><input type="text" name="txt1"></td>
</tr>
<tr>
<td>Designation</td><td><input type="text" name="txt2"></td>
</tr>
<tr>
<td>Sal</td><td><input type="text" name="txt3"></td>
</tr>


                        www.vineetsaini.wordpress.com
PHP Tutorials By Vineet Kumar Saini

<tr>
<td>Qualification</td><td><input type="text" name="txt4"></td>
</tr>
</table>
</center>
</body>
</html>

Output

In the above code we created a table named employee. There are four fields in the
employee table i.e. name, designation, sal, qualification.




Insert Data in Table

Now we will write the code to insert data into the database table. The code for inserting
data into the database table is as follows:

<?php
$con=mysql_connect ("localhost","root","");
mysql_select_db("mcn",$con);
@$a=$_POST['txt1'];
@$b=$_POST['txt2'];
@$c=$_POST['txt3'];
@$d=$_POST['txt4'];
if(@$_POST['inser'])
{
 $s="insert into employee values ('$a','$b','$c','$d')";
echo "Your Data Inserted";
 mysql_query ($s);
}
$con=mysql_connect ("localhost","root","");
mysql_select_db ("mcn",$con);
if(@$_POST ['sel'])



                             www.vineetsaini.wordpress.com
PHP Tutorials By Vineet Kumar Saini

{
echo $ins=mysql_query ("select * from employee");
echo "<table bgcolor=skyblue border='2'>
  <tr>
  <th colspan=4>Select Data From Employee Table</th></tr>
  <tr>
  <th>Nmae</th>
  <th>Designation</th>
  <th>Sal</th>
  <th>Qualification</th>
  </tr>";
  while ($row=mysql_fetch_array ($ins))
  {
  echo "<tr>";
  echo "<th>".$row ['Name']."</th>";
  echo "<th>". $row ['Designation']. "</th>";
  echo "<th>". $row ['Sal']."</th>";
  echo "<th>". $row ['Qualification']."</th>";
  echo "</tr>";
  }
  }
  echo "</table>"
?>
<html>
<head>
</head>
<body bgcolor="pink">
<table bgcolor="skyblue" border="2">
<form method="post">
<tr>
<td colspan=2 align="center">Employee Table</td>
</tr>
<td>Name</td><td><input type="text" name="txt1"></td>
</tr>
<tr>
<td>Designation</td><td><input type="text" name="txt2"></td>
</tr>
<tr>
<td>Sal</td><td><input type="text" name="txt3"></td>
</tr>
<tr>
<td>Qualification</td><td><input type="text" name="txt4"></td>
</tr>
<tr>
<td><input type="submit" name="inser" value="Insert"></td>
<td><input type="submit" name="sel" value="Select"></td>
</tr>
</form>
</table>
</body>
</html

Output


                        www.vineetsaini.wordpress.com
PHP Tutorials By Vineet Kumar Saini

Now we will insert the data into a HTML table then click on the insert button.




When you click on the insert button then your data will be inserted in the database table.
Like as in the following image.




Select Data From Database

If you want to select all data from the database into the HTML table then you can do that.
For this purpose you will be click on the select button. When you click on the select button
then your data will be displayed in the HTML table, such as in the following image:




                            www.vineetsaini.wordpress.com
PHP Tutorials By Vineet Kumar Saini




Conclusion

So in this article you saw, how to insert and select data from a database into a HTML table.
Using this article one can easily understand insert and select data from database in to HTML
table.




                           www.vineetsaini.wordpress.com

Mais conteúdo relacionado

Mais procurados

Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
Harit Kothari
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02
Hassen Poreya
 
Caracteristicas Basicas De Htlm
Caracteristicas Basicas De HtlmCaracteristicas Basicas De Htlm
Caracteristicas Basicas De Htlm
Maria S Rivera
 
HTML Powerpoint-Jeffrey C. Johnson III
HTML Powerpoint-Jeffrey C. Johnson IIIHTML Powerpoint-Jeffrey C. Johnson III
HTML Powerpoint-Jeffrey C. Johnson III
jeffcarlj
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
mrcoffee282
 

Mais procurados (20)

Form Processing In Php
Form Processing In PhpForm Processing In Php
Form Processing In Php
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02
 
Why study java script
Why study java scriptWhy study java script
Why study java script
 
Form Handling using PHP
Form Handling using PHPForm Handling using PHP
Form Handling using PHP
 
Making web forms using php
Making web forms using phpMaking web forms using php
Making web forms using php
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Caracteristicas Basicas De Htlm
Caracteristicas Basicas De HtlmCaracteristicas Basicas De Htlm
Caracteristicas Basicas De Htlm
 
ملخص تقنية تصميم صفحات الويب - الوحدة الرابعة
ملخص تقنية تصميم صفحات الويب - الوحدة الرابعةملخص تقنية تصميم صفحات الويب - الوحدة الرابعة
ملخص تقنية تصميم صفحات الويب - الوحدة الرابعة
 
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
PromptWorks Talk Tuesdays: Ray Zane 1/17/17 "Elixir Is Cool"
 
Idoc script beginner guide
Idoc script beginner guide Idoc script beginner guide
Idoc script beginner guide
 
ملخص تقنية تصميم صفحات الويب - الوحدة الخامسة
ملخص تقنية تصميم صفحات الويب - الوحدة الخامسةملخص تقنية تصميم صفحات الويب - الوحدة الخامسة
ملخص تقنية تصميم صفحات الويب - الوحدة الخامسة
 
HTML Powerpoint-Jeffrey C. Johnson III
HTML Powerpoint-Jeffrey C. Johnson IIIHTML Powerpoint-Jeffrey C. Johnson III
HTML Powerpoint-Jeffrey C. Johnson III
 
Web technology lab manual
Web technology lab manualWeb technology lab manual
Web technology lab manual
 
6. Php MongoDB adaugarea unui document
6. Php MongoDB adaugarea unui document6. Php MongoDB adaugarea unui document
6. Php MongoDB adaugarea unui document
 
PHP - Introduction to PHP Forms
PHP - Introduction to PHP FormsPHP - Introduction to PHP Forms
PHP - Introduction to PHP Forms
 
PHP Form Validation Technique
PHP Form Validation TechniquePHP Form Validation Technique
PHP Form Validation Technique
 
Rog rog. html
Rog rog. htmlRog rog. html
Rog rog. html
 
Week 12 code
Week 12 codeWeek 12 code
Week 12 code
 
9. Php MongoDB cautarea unui document
9. Php MongoDB cautarea unui document9. Php MongoDB cautarea unui document
9. Php MongoDB cautarea unui document
 
Ôn tập KTTMDT
Ôn tập KTTMDTÔn tập KTTMDT
Ôn tập KTTMDT
 

Semelhante a Database connectivity in PHP

Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
Mouli Chandira
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
Anthony Montalbano
 

Semelhante a Database connectivity in PHP (20)

Ex[1].3 php db connectivity
Ex[1].3 php db connectivityEx[1].3 php db connectivity
Ex[1].3 php db connectivity
 
Lect# 1 html part ii
Lect# 1 html part iiLect# 1 html part ii
Lect# 1 html part ii
 
Lab final
Lab finalLab final
Lab final
 
Intodcution to Html
Intodcution to HtmlIntodcution to Html
Intodcution to Html
 
1cst
1cst1cst
1cst
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
HTML5 New and Improved
HTML5   New and ImprovedHTML5   New and Improved
HTML5 New and Improved
 
INTRODUCTION TO HTML & CSS .pptx
INTRODUCTION TO HTML & CSS .pptxINTRODUCTION TO HTML & CSS .pptx
INTRODUCTION TO HTML & CSS .pptx
 
Polymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web componentsPolymer - pleasant client-side programming with web components
Polymer - pleasant client-side programming with web components
 
Tutorial asp.net
Tutorial  asp.netTutorial  asp.net
Tutorial asp.net
 
Practicals it
Practicals itPracticals it
Practicals it
 
16-DOMTree.pptx
16-DOMTree.pptx16-DOMTree.pptx
16-DOMTree.pptx
 
Your Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages SuckYour Custom WordPress Admin Pages Suck
Your Custom WordPress Admin Pages Suck
 
HTML 4.0
HTML 4.0HTML 4.0
HTML 4.0
 
Learn JavaScript HTML & CSS
Learn JavaScript HTML & CSSLearn JavaScript HTML & CSS
Learn JavaScript HTML & CSS
 
Lectuer html2
Lectuer  html2Lectuer  html2
Lectuer html2
 
Html ppt
Html pptHtml ppt
Html ppt
 
Create Components in TomatoCMS
Create Components in TomatoCMSCreate Components in TomatoCMS
Create Components in TomatoCMS
 
Html file
Html fileHtml file
Html file
 
Html and css
Html and cssHtml and css
Html and css
 

Mais de Vineet Kumar Saini

Mais de Vineet Kumar Saini (20)

Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Country State City Dropdown in PHP
Country State City Dropdown in PHPCountry State City Dropdown in PHP
Country State City Dropdown in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 

Último

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Último (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

Database connectivity in PHP

  • 1. PHP Tutorials By Vineet Kumar Saini Database Connectivity in PHP This article explains how to insert and select data from a database into a HTML table in PHP. Using an insert statement you can insert data into a database. Using a select statement you can select the entire data from a database into a HTML table. First of all we will create a database. Create The Database <html> <body bgcolor="pink"> <center> <?php $con = mysql_connect ("localhost","root",""); $db="MCN"; if (!$con) { die ('Could not connect: ' . mysql_error()); } if (mysql_query ("CREATE DATABASE $db",$con)) { echo "Your Database Created Which Name is : $db"; } else { echo "Error creating database: " . mysql_error(); } mysql_close ($con); ?> </center> }</body> </html> Output In the above code, we created a database named MCN. Now we will create a table in the MCN database. www.vineetsaini.wordpress.com
  • 2. PHP Tutorials By Vineet Kumar Saini Create The Table <html> <body bgcolor="pink"> <center> <?php $con = mysql_connect ("localhost","root",""); if (!$con) { die ('Could not connect: ' . mysql_error()); } mysql_select_db ("MCN", $con); $sql = "CREATE TABLE EMPLOYEE ( Name varchar(50), Designation varchar(50), Sal int, Qualification varchar(50) )"; mysql_query($sql,$con); echo "Your table created which is as follows"; mysql_close($con); ?> <table bgcolor="skyblue" border="2"> <tr> <td colspan=2 align="center">Employee Table</td> </tr> <td>Name</td><td><input type="text" name="txt1"></td> </tr> <tr> <td>Designation</td><td><input type="text" name="txt2"></td> </tr> <tr> <td>Sal</td><td><input type="text" name="txt3"></td> </tr> www.vineetsaini.wordpress.com
  • 3. PHP Tutorials By Vineet Kumar Saini <tr> <td>Qualification</td><td><input type="text" name="txt4"></td> </tr> </table> </center> </body> </html> Output In the above code we created a table named employee. There are four fields in the employee table i.e. name, designation, sal, qualification. Insert Data in Table Now we will write the code to insert data into the database table. The code for inserting data into the database table is as follows: <?php $con=mysql_connect ("localhost","root",""); mysql_select_db("mcn",$con); @$a=$_POST['txt1']; @$b=$_POST['txt2']; @$c=$_POST['txt3']; @$d=$_POST['txt4']; if(@$_POST['inser']) { $s="insert into employee values ('$a','$b','$c','$d')"; echo "Your Data Inserted"; mysql_query ($s); } $con=mysql_connect ("localhost","root",""); mysql_select_db ("mcn",$con); if(@$_POST ['sel']) www.vineetsaini.wordpress.com
  • 4. PHP Tutorials By Vineet Kumar Saini { echo $ins=mysql_query ("select * from employee"); echo "<table bgcolor=skyblue border='2'> <tr> <th colspan=4>Select Data From Employee Table</th></tr> <tr> <th>Nmae</th> <th>Designation</th> <th>Sal</th> <th>Qualification</th> </tr>"; while ($row=mysql_fetch_array ($ins)) { echo "<tr>"; echo "<th>".$row ['Name']."</th>"; echo "<th>". $row ['Designation']. "</th>"; echo "<th>". $row ['Sal']."</th>"; echo "<th>". $row ['Qualification']."</th>"; echo "</tr>"; } } echo "</table>" ?> <html> <head> </head> <body bgcolor="pink"> <table bgcolor="skyblue" border="2"> <form method="post"> <tr> <td colspan=2 align="center">Employee Table</td> </tr> <td>Name</td><td><input type="text" name="txt1"></td> </tr> <tr> <td>Designation</td><td><input type="text" name="txt2"></td> </tr> <tr> <td>Sal</td><td><input type="text" name="txt3"></td> </tr> <tr> <td>Qualification</td><td><input type="text" name="txt4"></td> </tr> <tr> <td><input type="submit" name="inser" value="Insert"></td> <td><input type="submit" name="sel" value="Select"></td> </tr> </form> </table> </body> </html Output www.vineetsaini.wordpress.com
  • 5. PHP Tutorials By Vineet Kumar Saini Now we will insert the data into a HTML table then click on the insert button. When you click on the insert button then your data will be inserted in the database table. Like as in the following image. Select Data From Database If you want to select all data from the database into the HTML table then you can do that. For this purpose you will be click on the select button. When you click on the select button then your data will be displayed in the HTML table, such as in the following image: www.vineetsaini.wordpress.com
  • 6. PHP Tutorials By Vineet Kumar Saini Conclusion So in this article you saw, how to insert and select data from a database into a HTML table. Using this article one can easily understand insert and select data from database in to HTML table. www.vineetsaini.wordpress.com