2. PHP MySQL IntroductionPHP MySQL Introduction
MySQL is the most popular open source
database server.
The free MySQL Database is very often
used with PHP.
Before you can access and work with data
in a database, you must create a
connection to the database.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
3. PHP MySQL IntroductionPHP MySQL Introduction
A database defines a structure for storing
information.
In a database, there are tables. Just like HTML
tables, database tables contain rows, columns,
and cells.
Databases are useful when storing information
categorically. A company may have a database
with the following tables: "Employees",
"Products", "Customers" and "Orders".
MOHAMAD RAHIMI MOHAMAD
ROSMAN
4. PHP MySQL Connect to a DatabasePHP MySQL Connect to a Database
In PHP, this is done with the
mysql_connect() function.
Syntax
mysql_connect(servername,username,password)
;
mysql_connect($dbHost,$dbUser,$dbPass)or die(mysql_error());
mysql_select_db($dbName);
MOHAMAD RAHIMI MOHAMAD
ROSMAN
5. Include databaseInclude database
To prevent
frequent
access to
database and
shorten the
code, we
should use
include()
within all the
files
MOHAMAD RAHIMI MOHAMAD
ROSMAN
6. Closing a ConnectionClosing a Connection
The connection will be closed as soon as the
script ends. To close the connection before, use
the mysql_close() function.
<?php
$con = mysql_connect("localhost",“root",“is221");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
MOHAMAD RAHIMI MOHAMAD
ROSMAN
7. QueriesQueries
A query is a question or a request.
With MySQL, we can query a database
for specific information and have a
recordset returned.
Look at the following query:
MOHAMAD RAHIMI MOHAMAD
ROSMAN
9. Creating databaseCreating database
Open PHPMyAdmin
Create new database name “php”
Create new table name “user” (5 fields)
Insert following fields
◦ id (bigint, auto increment)
◦ Nama (text)
◦ Ic (text)
◦ Username (text)
◦ Password (text)
MOHAMAD RAHIMI MOHAMAD
ROSMAN
10. Connecting to the databaseConnecting to the database
Create a new text document, name it index.php
Insert the following code
<html><body>
<?php
mysql_connect(“localhost”,”root”,””)or die(mysql_error());
mysql_select_db(“php”);
?>
</body></html>
MOHAMAD RAHIMI MOHAMAD
ROSMAN
11. Insert Data From a Form Into a DatabaseInsert Data From a Form Into a Database
Now we will create an HTML form that can be
used to add new records to the "Person"
table.
Here is the HTML form:
MOHAMAD RAHIMI MOHAMAD
ROSMAN
12. Step 1: Create HTML FormStep 1: Create HTML Form
MOHAMAD RAHIMI MOHAMAD
ROSMAN
13. When a user clicks the submit button in the
HTML form in the example above, the form data
is sent to "insert.php".
The "insert.php" file connects to a database, and
retrieves the values from the form with the PHP
$_POST variables.
Then, the mysql_query() function executes the
INSERT INTO statement, and a new record will
be added to the database table.
Below is the code in the "insert.php" page:
MOHAMAD RAHIMI MOHAMAD
ROSMAN
15. Select Data From a Database TableSelect Data From a Database Table
The SELECT statement is used to select
data from a database.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
18. The WHERE clauseThe WHERE clause
To select only data that matches a specific
criteria, add a WHERE clause to the
SELECT statement.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
23. Sort Ascending or DescendingSort Ascending or Descending
If you use the ORDER BY keyword, the
sort-order of the recordset is ascending by
default (1 before 9 and "a" before "p").
Use the DESC keyword to specify a
descending sort-order (9 before 1 and "p"
before "a"):
MOHAMAD RAHIMI MOHAMAD
ROSMAN
24. Update Data In a DatabaseUpdate Data In a Database
The UPDATE statement is used to
modify data in a database table
MOHAMAD RAHIMI MOHAMAD
ROSMAN
26. Delete Data In a DatabaseDelete Data In a Database
The DELETE FROM statement is used to
delete records from a database table.
MOHAMAD RAHIMI MOHAMAD
ROSMAN
28. ~…The end …~~…The end …~
Thank You Very Much!!
For further questions, comments & suggestion, contact me at:
MOHAMAD RAHIMI BIN MOHAMAD ROSMAN
09-976 2275 / 019-9891306
rahimi.rosman@yahoo.com
http://rahimi.official.ws
http://is110.official.ws
MOHAMAD RAHIMI MOHAMAD
ROSMAN