Embed
Email

php_mysql

Document Sample

Shared by: cuiliqing
Categories
Tags
Stats
views:
54
posted:
10/31/2011
language:
English
pages:
13
PHP and MySQL

After this lecture, you should be able to:

 Access a MySQL database with PHP

 mysql_connect()

 mysql_select_db()

 mysql_query()

 mysql_fetch_array()

 mysql_fetch_object()



 Complete the Assignment 5.









PHP MySQL 1

Accessing a MySQL Database

with a PHP Script



1

Client Apache

Browser Web Server



6

2 5 MySQL



3

PHP Module DB

4









PHP MySQL 2

MySQL Functions





 Prefixed with mysql_

 mysql_connect()

 mysql_select_db()

 mysql_query()

 etc.

 http://www.php.net/manual/en/ref.mysql.php









PHP MySQL 3

Connecting to a Database













PHP MySQL 4

Common Include File: common.inc

\n");

mysql_select_db(DB_NAME)

or die ("Unable to select database"

. DB_NAME . "\n");

echo "Connected to database!”;

?>



PHP MySQL 5

Retrieving Records





$query =

"select * from s";

$result =

mysql_query($query);









PHP MySQL 6

Displaying Retrieved Records

while ($record = mysql_fetch_array($result)) {

echo “Supplier Number: “ . $record[„sno‟] . “\n”;

echo “Supplier Name: “ . $record[„sname‟] . “\n”;

echo “Supplier Status: “ . $record[„status‟] . “\n\n”;

}



Connected to database!

Supplier Number: s1

Supplier Name: Smith

Supplier Status: 20



Supplier Number: s2

Supplier Name: Jones

Supplier Status: 10



PHP MySQL 7

Displaying Retrieved Records



while ($record = mysql_fetch_object($result))

{

echo “Supplier Number: $record->sno\n”;

echo “Supplier Name: $record->sname\n”;

echo “Supplier Status: $record->status\n\n”;

}









PHP MySQL 8

Inserting a Record



$query =

"insert into s(sno, sname)

values(„s10', „Bose‟);

$result =

mysql_query($query);

if (!$result) {

echo “Insertion failed”;

}







PHP MySQL 9

Updating Records



$query = "update s

set city = „Albany'

where sname = „Bose'";

$result =

mysql_query($query);

if (!$result) {

echo “Update failed”;

}





PHP MySQL 10

Deleting Records



$query = "delete from s

where status









PHP MySQL 12

Example: get_item()











PHP MySQL 13



Related docs
Other docs by cuiliqing
7 Recipes from Joe A.
Views: 2  |  Downloads: 0
Re-installingXPMode
Views: 3  |  Downloads: 0
telefonica_en
Views: 4  |  Downloads: 0
3220 Chap 6 demos
Views: 2  |  Downloads: 0
chap history.docx
Views: 3  |  Downloads: 0
Subcontractor Bid Form - The Fountains
Views: 1  |  Downloads: 0
English
Views: 1  |  Downloads: 0
DESIGNER'S SCHEDULE USE
Views: 1  |  Downloads: 0
Security Service Providers
Views: 45  |  Downloads: 0
By registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!