Select Insert Update Delete Record using PHP and MySQL


 

         

1)Create:- The CREATE DATABASE statement is used to create a
database in MySQL.

Syntax
CREATE DATABASE database_name

Exmaple:-
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>

2)Insert:- The INSERT INTO statement is used to add new records to a
database table.

Syntax:-It is possible to write the INSERT INTO statement in two
forms.
The first form doesn't specify the column names where the data will be
inserted, only their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be
inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

Example:-
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_close($con);
?>

3)Select:- The SELECT statement is used to select data from a database.

Syntax
SELECT column_name(s)
FROM table_name
To get PHP to execute the statement above we must use the
mysql_query() function. This function is used to send a query or
command to a MySQL connection.
Example:-The following example selects all the data stored in the
"Persons" table (The * character selects all the data in the table):

<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
while($row = mysql_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}
mysql_close($con);
?>

4)Delete:- The DELETE FROM statement is used to delete records from a
database table.

Syntax
DELETE FROM table_name
WHERE some_column = some_value
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause
specifies which record or records that should be deleted. If you omit the
WHERE clause, all records will be deleted!
To get PHP to execute the statement above we must use the
mysql_query() function. This function is used to send a query or
command to a MySQL connection.

Example:-
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
mysql_close($con);
?>

5)Update:- The UPDATE statement is used to update existing records in
a table.

Syntax
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause
specifies which record or records that should be updated. If you omit the
WHERE clause, all records will be updated!
To get PHP to execute the statement above we must use the
mysql_query() function. This function is used to send a query or
command to a MySQL connection.


Example
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("UPDATE Persons SET Age = '36'
WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
mysql_close($con);
?>

Post a Comment

0 Comments