Vous êtes sur la page 1sur 16

Database

MySQL Introduction
MySQL Connect MySQL Create

MySQL Insert
MySQL Select MySQL Where MySQL Order By

MySQL Update
MySQL Delete

MySQL is the most popular open-source database system. What is MySQL?


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

Create a Connection to a MySQL Database


Before you can access data in a database, you must create a connection to the database.

In PHP, this is done with the mysql_connect() function.

Syntax
mysql_connect( servername, username, password ); / mysql_close($con); <?php $con =mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code
mysql_close($con);

?>

Create a Database
The CREATE DATABASE statement is used to create a database in MySQL.

Syntax
CREATE DATABASE database_name

Create a Table The CREATE TABLE statement is used to create a table in MySQL.
Syntax CREATE TABLE table_name ( column_name1 data_type, column_name2 data_type, column_name3 data_type, .... )

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

// Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql , $con); mysql_close($con); ?>

// Create database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); }

Insert Data Into a Database Table


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,...)

<html> <body>

<?php $con = mysql_connect("localhost","peter","abc123"); <form action="insert.php" method="post"> if (!$con) Firstname: <input type="text" { name="firstname" /> die('Could not connect: ' . mysql_error()); Lastname: <input type="text" } name="lastname" /> Age: <input type="text" name="age" /> mysql_select_db("my_db", $con); <input type="submit" /> </form> $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES </body> ('$_POST[firstname]','$_POST[lastname]','$_POST[age] </html> ')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";

mysql_close($con) ?>

Select Data From a Database Table


The SELECT statement is used to select data from a database.

Syntax SELECT column_name(s) FROM table_name


The WHERE clause The WHERE clause is used to extract only those records that fulfill a specified criterion. Syntax
SELECT column_name(s) FROM table_name WHERE column_name operator value

<?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); ?> The output of the code will be: Ilambaruthi Jayabalan Raj kumar

<?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 WHERE FirstName=Ilambaruthi'");

while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } ?> The output of the code will be: Ilambaruthi Jayabalan

The ORDER BY Keyword


The ORDER BY keyword is used to sort the data in a recordset.

The ORDER BY keyword sort the records in ascending order by default.


If you want to sort the records in a descending order, you can use the DESC keyword. Syntax
SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC

<?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 ORDER BY age");

Order by Two Columns SELECT column_name(s) FROM table_name ORDER BY column1, column2

while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; The output of the code will be: echo "<br />"; } Anil Ambani 33 mysql_close($con); ?> Mukesh Ambani 35

Update Data In a Database


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 Firstname Anil Lastname Ambani Age 33

BEFORE

Mukesh

Ambani

35

Firstname

Lastname

Age

Anil
Mukesh

Ambani
Ambani

33
36

AFTER

<?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 = ' Mukesh' AND LastName = ' Ambani'");

mysql_close($con); ?>

Delete Data In a Database The DELETE FROM statement is used to delete records from a database table. Syntax
DELETE FROM table_name WHERE some_column = some_value

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) First { die('Could not connect: ' . mysql_error()); name }
Anil

Last name Ambani Ambani

Age 33 36

mysql_select_db("my_db", $con);

Mukesh

mysql_query("DELETE FROM Persons WHERE Firstname=Mukesh'");


mysql_close($con); ?>
Anil Ambani 33

Vous aimerez peut-être aussi