0% found this document useful (0 votes)
58 views20 pages

Unit-6

This document discusses connecting a PHP application to a MySQL database. It covers creating and selecting databases, creating and modifying tables, inserting, updating, and deleting data. Key points covered include using mysql_connect() to establish a connection, mysql_query() to execute commands, and mysql_close() to close the connection. Examples are provided for common CRUD operations like creating tables, inserting data, updating records, and dropping databases.

Uploaded by

Super hero
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)
58 views20 pages

Unit-6

This document discusses connecting a PHP application to a MySQL database. It covers creating and selecting databases, creating and modifying tables, inserting, updating, and deleting data. Key points covered include using mysql_connect() to establish a connection, mysql_query() to execute commands, and mysql_close() to close the connection. Examples are provided for common CRUD operations like creating tables, inserting data, updating records, and dropping databases.

Uploaded by

Super hero
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/ 20

WEB DEVLOPMENT

(3151606)

UNIT-6:
DATABASE
CONECTIVITY
USING PHP

PREPARED BY: PROF. KRUPALI. DAVE


DATABASE:

❑ MySQL is a database.

❑ You can download MySQL from http://dev.mysql.com/downloads/mysql.

❑ The data in MySQL is stored in database objects called tables.

❑ A table is a collection of related data entries and it consists of columns and rows.

❑ A query is a request to database for specific information


DATABASE CREATION:

1.) Creating Database:


mysql> CREATE DATABASE mydb;

2.) Displaying all the database:


mysql> SHOW DATABASES;
O/P: Database
mydb
mysql
Student
test
CONT…

3.) Selecting Particular database:


mysql> USE MYDB;

4.) Creating Table:


mysql> create table student (id int(4),name varchar(20));

5.) Display a table:


mysql> show tables;
CONT…

6.) Display the table fields:


mysql> describe student;
O/P : Field Type null
id int(4) yes
name varchar(20) yes

7.) Insert value in the table:


Insert into student(id,name)values(1,’abc’);
CONT…

8.) Display the content of the table:


Select * from student;

9.) Updating the record:


Update student set name=‘pqr’ where id=1;
CONT…

10.) Deleting the record:


Delete from student where id=1;

11.) Deleting the table:


Drop table student;
DELETING COOKIE:

❑ When deleting a cookie you should assure that the expiration date is in the past.

Ex:
<?php
// set the expiration date to one hour ago
setcookie ("user", "", time()-3600);
?>
CONNECTION TO
SERVER(CRUD EXAMPLE):
❑ mysql_connect() function is used to establish the connection between PHP and
MYSQL.

❑ Syntax of mysql_connect() is,


mysql_connect (“servername”,”username”, “password”);
Ex:
<?php
$con = mysql_connect ("localhost“, “sa", “shree“);
if (!$con)
{
die ('Could not connect: ' .mysql_error());
}
// some code
?>

❑ The mysql_error() function is used to obtain the error messages if any command
gets failed
CLOSING DATABASE:

❑ The database can be closed using the function mysql_close();


Ex: <?php
$con = mysql_connect("localhost",“sa",“shree");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

// some code

mysql_close ($con);
?>
CREATING DATABASE

❑ Using mysql_query() function we can create a database.

❑ Ex:
$con=mysql_connect (“servername”, ”sa”, ”shree”);
mysql_query (“CREATE DATABASE student”, $con);
SELECTING DATABASE

❑ Mysql_select_db (“student”, $con);

❑ Listing Database:
Mysql_list_db($con);

❑ Listing Table Names:


Mysql_list_tables(); or mysql_query();
CREATE TABLE:

<? php
// Create table
mysql_select_db (“student", $con);
$sql = "CREATE TABLE Student_Data
(
Id int(5),
Name varchar(15),
Age int;
)";
mysql_query($sql,$con); // Execute query
mysql_close($con); //close database
?>
INSERTING DATA:

❑ The INSERT INTO statement is used to insert new records in a table.

❑ Ex:
<? Php
mysql_select_db (“student", $con);
$q= ("INSERT INTO Student_Data (Id, Name, Age) VALUES (‘1', ‘XYZ',
‘20')"); mysql_query ($q, $con);
mysql_close ($con);
?>
ALTERING TABLE:

❑ This command is useful to add, delete, changing the size of some columns or
change the name of some column.

❑ Ex:
mysql_select_db (“mydb”, $con);
$query=“Alter Table student add column address varchar(30)”;
mysql_query ($query, $con); // Execution query
mysql_close($con); //closing the database
DROP DATABASE:

❑ We can drop database using the Drop query.

❑ Ex:
my_sql_db (“mydb”, $con); //select the database
$query=“DROP DATABASE mydb”;
mysql_query($query,$con);//Execution of query
mysql_close($con); //closing the database
Example(retrieve data from table):

Mysql.php output
Example(update):

output
Mysqlupdate.php
THANK YOU…

You might also like