0% found this document useful (0 votes)
7 views12 pages

Unit-6-Connectivity With PHP and MYSQL

This document provides examples of database programming using PHP and MySQL, including establishing connections, creating databases and tables, and performing CRUD operations (Create, Read, Update, Delete). It includes code snippets for inserting, updating, and deleting records, as well as using HTML forms for data input. The document also demonstrates the use of GET method for retrieving data from user input.

Uploaded by

vaghanikaram1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

Unit-6-Connectivity With PHP and MYSQL

This document provides examples of database programming using PHP and MySQL, including establishing connections, creating databases and tables, and performing CRUD operations (Create, Read, Update, Delete). It includes code snippets for inserting, updating, and deleting records, as well as using HTML forms for data input. The document also demonstrates the use of GET method for retrieving data from user input.

Uploaded by

vaghanikaram1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-5

Databse Programming with PHP and Mysql

Connection established with php and mysql


Example:
<?php
$conn=mysqli_connect("localhost:3307","root","");

if($conn)
{
echo "connection established successfully..." ;
}
else
{
die("connection failed...");
}
?>

Create database using php function


Example:
<?php

Web Programming (3360713) Page 1 of 12


$conn=mysqli_connect("localhost:3307","root","");

if($conn)
{
echo "connection established successfully..." ;
$qry="create database tt";
$ans=mysqli_query($conn,$qry);
if($ans)
{
echo "database created successfully..." ;
}
}
else
{
die("error...");
}
?>

Web Programming (3360713) Page 2 of 12


Create table using php function in mysql database
Example:
<?php
$conn=mysqli_connect("localhost:3307","root","","tt");

if($conn)
{
echo "connection established successfully..." ;
$qry="create table t1(no int Primary Key, name varchar(30),city varchar(30))";
$ans=mysqli_query($conn,$qry);
if($ans)
{
echo "Table created successfully..." ;
}

Web Programming (3360713) Page 3 of 12


}
else
{
die("error...");
}
?>

Web Programming (3360713) Page 4 of 12


Insert record in mysql database using php
Example:
<?php
$conn=mysqli_connect("localhost:3307","root","","tt");

if($conn)
{
$qry="insert into t1 values(1,'Trupti','Surat')";
$ans=mysqli_query($conn,$qry);
if($ans)
{
echo "Record inserted successfully..." ;
}
}
else
{
die("error...");
}
?>

Web Programming (3360713) Page 5 of 12


Update record in database using php function
Example:
<?php
$conn=mysqli_connect("localhost:3307","root","","tt");

if($conn)
{
$qry="update t1 set name='Devi' where no=2";
$ans=mysqli_query($conn,$qry);
if($ans)
{
echo "Record updated successfully..." ;
}
}
else
{
die("error...");
}
?>

Web Programming (3360713) Page 6 of 12


After updated:

Before updated:

Web Programming (3360713) Page 7 of 12


Delete record from the database
Example:
<?php
$conn=mysqli_connect("localhost:3307","root","","tt");

if($conn)
{
$qry="delete from t1 where no=3";
$ans=mysqli_query($conn,$qry);
if($ans)
{
echo "Record deleted successfully..." ;
}
}
else
{
die("error...");
}
?>

Web Programming (3360713) Page 8 of 12


Insert record in database using interface
Example:
Insertrecord.html File:
<html>
<body>
<form method="POST" action="addrecord.php">
Customer Code:<input type="text" name="txtcode"><br><br>
Customer Name:<input type="text" name="txtcname"><br><br>
<input type="submit" name=“submit” value="submit">

Web Programming (3360713) Page 9 of 12


</form>
</body>
</html>
Addrecord.php File:
<?php
$code=$_POST['txtcode'];
$name=$_POST['txtcname'];

$conn=mysqli_connect('localhost:3307','root',' ');
mysqli_select_db($conn,'tt');

$qry="insert into customer_detail values('.$code.','.$name.')";

mysqli_query($conn,$qry);

mysqli_close($conn);

if($conn)
{
echo "Record inserted successfully….";
}
else{
die('error....');
}
?>

Web Programming (3360713) Page 10 of 12


Get Method Example:
Example:
Getmethod_html.html File:
<html>
<body>
<form action="getmethod.php" method="GET">
Enter name:<input type="text" name="t1">
<input type="submit" value="OK">
</form>
</body>
</html>

Web Programming (3360713) Page 11 of 12


Getmethod.php File:

<?php

$name=$_GET['t1'];
echo "Welcome ".$name."....!<br><br>";
echo "Hello".$_GET['t1'];

?>

Web Programming (3360713) Page 12 of 12

You might also like