+ All Categories
Home > Data & Analytics > Chapter 09 php my sql

Chapter 09 php my sql

Date post: 21-Jul-2015
Category:
Upload: dhani-ahmad
View: 51 times
Download: 0 times
Share this document with a friend
28
PHP MySQL PHP MySQL Chapter 09 MOHAMAD RAHIMI MOHAMAD ROSMAN
Transcript
Page 1: Chapter 09   php my sql

PHP MySQLPHP MySQLChapter 09

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 2: Chapter 09   php my sql

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

Page 3: Chapter 09   php my sql

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

Page 4: Chapter 09   php my sql

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

Page 5: Chapter 09   php my sql

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

Page 6: Chapter 09   php my sql

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

Page 7: Chapter 09   php my sql

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

Page 8: Chapter 09   php my sql

Data TypesData TypesMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 9: Chapter 09   php my sql

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

Page 10: Chapter 09   php my sql

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

Page 11: Chapter 09   php my sql

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

Page 12: Chapter 09   php my sql

Step 1: Create HTML FormStep 1: Create HTML FormMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 13: Chapter 09   php my sql

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

Page 14: Chapter 09   php my sql

Insert.phpInsert.phpMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 15: Chapter 09   php my sql

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

Page 16: Chapter 09   php my sql

ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 17: Chapter 09   php my sql

ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 18: Chapter 09   php my sql

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

Page 19: Chapter 09   php my sql

OperatorOperatorMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 20: Chapter 09   php my sql

ExampleExampleMOHAMAD RAHIMI MOHAMAD ROSMAN

Page 21: Chapter 09   php my sql

The ORDER BY KeywordThe ORDER BY Keyword

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

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 22: Chapter 09   php my sql

ExampleExample

$result = mysql_query("SELECT * FROM user ORDER BY

nama"); $result = mysql_query("SELECT * FROM user ORDER BY

username");

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 23: Chapter 09   php my sql

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

Page 24: Chapter 09   php my sql

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

Page 25: Chapter 09   php my sql

ExampleExample

mysql_query("UPDATE user SET username= ‘user‘ WHERE ic = ‘1'");

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 26: Chapter 09   php my sql

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

Page 27: Chapter 09   php my sql

ExampleExample

mysql_query("DELETE FROM user WHERE ic=‘1'");

MOHAMAD RAHIMI MOHAMAD ROSMAN

Page 28: Chapter 09   php my sql

~…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


Recommended