The document describes how to connect a PHP script to a MySQL database and perform CRUD (create, read, update, delete) operations. It explains how to connect to the database, create tables, insert, update, delete and select data. Code examples are provided to create PHP files that connect to the database and perform each operation through a web form interface. The output displays the results of running the PHP scripts to interact with the database.
1. Ex. No.3 DATABASE CONNECTIVITY - PHP
28.01.2011
AIM:
To write PHP script for connecting to database and perform insert, update, delete and select
operations.
DESCRIPTION:
Create a Connection to a MySQL Database:
The mysql_connect() function is used to establish a connection to the database. The syntax
is:
mysql_connect(servername,username,password);
where
servername Optional. Specifies the server to connect to. Default value is "localhost:3306"
username Optional. Specifies the username to log in with. Default value is the name of
the user that owns the server process
password Optional. Specifies the password to log in with. Default is ""
Create a database:
A database holds one or multiple tables. The CREATE DATABASE statement is used to
create a database in MySQL. The syntax is:
CREATE DATABASE database_name
Create a Table:
The CREATE TABLE statement is used to create a table in MySQL. The syntax is:
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
Insert Data Into a Database Table:
The INSERT INTO statement is used to insert new records in a table. It is possible to write
the INSERT INTO statement in two forms:
INSERT INTO table_name VALUES (value1, value2, value3,...)
(or)
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
2. Update Data In a Database:
The UPDATE statement is used to update existing records in a table. The syntax is:
UPDATE table_name SET column1=value, column2=value2,...
WHERE some_column=some_value
Delete Data In a Database:
The DELETE FROM statement is used to delete records from a database table. The syntax is:
DELETE FROM table_name WHERE some_column = some_value
Select Data From a Database Table:
The SELECT statement is used to select data from a database. The syntax is:
SELECT column_name(s) FROM table_name
SOURCE CODE:
insertform.html:
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css"/>
</head>
<body>
<pre>
<h3>STUDENT INFORMATION SYSTEM</h3>
<form name="myForm" action="insert.php" method="post">
<table>
<tr><td>Name</td><td><input type="text" name="name"></td></tr>
<tr><td>Reg. No.</td><td><input type="text" name="reg"></td></tr>
<tr><td>Age</td><td><input type=text name="age" size=2/></td></tr>
<tr><td>Gender</td><td><input type="text" name="gender"</td></tr>
<tr><td>Email</td><td><input type=text name="email"/></td></tr>
<tr><td><b>Academic Details</b></td></tr>
<tr><td>Semester 1 GPA</td><td><input type=text name="sem1"></td></tr>
<tr><td>Semester 2 GPA</td><td><input type=text name="sem2"></td></tr>
<tr><td>CGPA</td><td><input type=text name="cgpa"></td></tr></table>
<center><input type="submit" value="Submit"/> <input type="reset"
value="Clear"/>
</form>
</body>
</html>