Chapter 09 php my sql
-
Upload
dhani-ahmad -
Category
Data & Analytics
-
view
55 -
download
0
Transcript of Chapter 09 php my sql
PHP MySQLPHP MySQLChapter 09
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
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
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
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
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
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
Data TypesData TypesMOHAMAD RAHIMI MOHAMAD ROSMAN
Creating databaseCreating database
Open PHPMyAdminCreate 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
Connecting to the databaseConnecting to the database
Create a new text document, name it index.php Insert the following code
<html><body><?phpmysql_connect(“localhost”,”root”,””)or die(mysql_error());mysql_select_db(“php”);
?></body></html>
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
Step 1: Create HTML FormStep 1: Create HTML FormMOHAMAD RAHIMI MOHAMAD ROSMAN
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
Insert.phpInsert.phpMOHAMAD RAHIMI MOHAMAD ROSMAN
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
ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN
ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN
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
OperatorOperatorMOHAMAD RAHIMI MOHAMAD ROSMAN
ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN
The ORDER BY KeywordThe ORDER BY Keyword
The ORDER BY keyword is used to sort the data in a recordset
MOHAMAD RAHIMI MOHAMAD ROSMAN
ExampleExample
$result = mysql_query("SELECT * FROM user ORDER BY
nama"); $result = mysql_query("SELECT * FROM user ORDER BY
username");
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
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
ExampleExample
mysql_query("UPDATE user SET username= ‘user‘ WHERE ic = ‘1'");
MOHAMAD RAHIMI MOHAMAD ROSMAN
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
ExampleExample
mysql_query("DELETE FROM user WHERE ic=‘1'");
MOHAMAD RAHIMI MOHAMAD ROSMAN
~…The end …~~…The end …~Thank You Very Much!!
For further questions, comments & suggestion, contact me at:
MOHAMAD RAHIMI BIN MOHAMAD ROSMAN09-976 2275 / 019-9891306
[email protected]://rahimi.official.wshttp://is110.official.ws
MOHAMAD RAHIMI MOHAMAD ROSMAN