SlideShare a Scribd company logo
PHP and MySQL.pptx
Building Data Dynamic Web Sites
 Truly dynamic web sites
 Content changes over time
 Content customised for individual user
 Content automatically generated
 Content Programmatically generated
 Can be File system based
 HTML and Images stored on File System
 Gets hard to manage over time
 Database based
 HTML, Images etc all generated from database
 Easier to manage
 If data is too large, can overload the database
PHP and MySQL.pptx
Database
• Structured collection of data.
• Tables
• Fields
• Query
• Reports
• Essentially a much more sophisticated
implementation of the flat files.
PHP and MySQL.pptx
Relational Database
• Stores data in separate tables instead of
a single store.
• Relationships between tables are set
• In theory, this provides a faster, more
flexible database system.
Example
• We wish to maintain a database of student names, IDs, addresses,
and any other information.
• Will be updated frequently with new names and information.
• Will want to retrieve data based on some predicate.
• e.g, ‘give me the names of all Massey students who live in
Albany’.
• Will want to update database with new information about students,
not previously recorded.
• e.g., may decide we want to include IRD nos.
• Very difficult to manage using ‘flat file’ systems
Databases
 Fast, Efficient back end storage
 Easier to manage than file system based approach
 Relational Database structure
 Well developed theory and practise
 Multi-user capable
 Multithreaded, multiprocessor, sometimes cluster
based systems
 Standards based queries
 Structured Query Language (SQL)
MySQL Database
 world's most popular open source database
because of its consistent fast performance, high
reliability and ease of use
 Open Source License:- free
 GNU General Public License
 Free to modify and distribute but all modification must
be available in source code format
 Commercial:- not free
 Fully paid up professional support
• used by Google, Facebook Nokia, YouTube,
Yahoo!, Alcatel-Lucent, Zappos.com, etc.
Basic Database Server Concepts
 Database runs as a server
 Attaches to either a default port or an administrator
specified port
 Clients connect to database
 For secure systems
 authenticated connections
 usernames and passwords
 Clients make queries on the database
 Retrieve content
 Insert content
 SQL (Structured Query Language) is the language used
to insert and retrieve content
• MySQL can be controlled through a
simple command-line interface; however,
we can use phpMyAdmin as an interface
to MySQL.
• phpMyAdmin is a very powerful tool; it
provides a large number of facilities for
customising a database management
system.
Client
Web
browser
Web
server
HTML
Server
MySQL
Operating System
PHP
interpreter
Internet
My codes
HTTP
TCP/IP
• Webserver supports HTTP.
Server: responds
phpMy
Admin
Create Database
Create Table: Customers
Specify the Table’s Fields &
Attributes: Customers
Table Edit Screen: Customers
Table: Products
Table: Products
Insert Record: Customers
PHP and MySQL.pptx
Connecting to a MySQL DBMS
• In order for our PHP script to access a
database we need to form a connection
from the script to the database
management system.
resourceId = mysql_connect(server, username, password);
• Server is the DBMS server
• username is your username
• password is your password
Connecting to a MySQL DBMS
• In order for our PHP script to access a
database we need to form a connection
from the script to the database
management system.
resourceId = mysql_connect(server, username, password);
• The function returns a resource-identifier type.
• a PHP script can connect to a DBMS anywhere in the world,
so long as it is connected to the internet.
• we can also connect to multiple DBMS at the same time.
Selecting a database
• Once connected to a DBMS, we can
select a database.
mysql_select_db(databasename, resourceId);
• the resourceId is the one returned by mysql_connect()
• the function returns true if the selection succeeded; false,
otherwise.
Example: Connect to a DBMS and
access database
<?php
$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
echo "<h1>Connected To Database</h1>";
?>
• die() stops execution of script if the database connection
attempt failed.
• mysql_error() returns an error message from the previous
MYSQL operation.
Reading from a database
• We can now send an SQL query to the
database to retrieve some data records.
resourceRecords = mysql_query(query, resourceId);
• the resourceId is the one returned by mysql_connect()
• the function returns a resource identifier to the returned data.
Example: Connect to a DBMS,
access database, send query
<?php
$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
$dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
echo "<h1>Connected To Database</h1>";
?>
• the function will return a resource pointer (not the actual
data) to all the records that match the query.
• If all goes well, this script will output nothing on screen.
Extract contents of one record
• We can now extract the actual data from
the resource pointer returned by
mysql_query().
fieldData= mysql_result(resourceRecords, row, field);
• the resourceRecords is the one returned by mysql_query()
• field – database field to return
• the function returns the data stored in the field.
Example: Connect to a DBMS,
access database, send query
<?php
$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
$dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
$strSurname = mysql_result($dbRecords, 0, "Surname");
echo "<p>$strSurname</p>";
?>
• the function will return a resource pointer (not the actual
data) to all the records that match the query.
• If all goes well, this script will output a surname on screen.
SQL statement
SELECT * FROM customers
• Go and obtain from the database
• every field
• FROM the
• customers table
Separating the database
connection
It is worth separating the database
connectivity from our scripts and placing
it in a separate file.
•It provides a convenient means of moving your scripts from
one database platform to another.
Example: Separating the database
connection
<?php
// File: database2.php
$strLocation = "Home";
//$strLocation = "Work";
if ($strLocation == "Home") {
$dbLocalhost = mysql_connect("localhost", "root", "")
or die("Could not connect: " . mysql_error());
mysql_select_db("glassesrus", $dbLocalhost)
or die("Could not find database: " . mysql_error());
} else {
$dbLocalhost = mysql_connect("localhost", "username", "password")
or die("Could not connect: " . mysql_error());
mysql_select_db("anotherdatabase", $dbLocalhost)
or die("Could not find database: " . mysql_error());
}
?>
• $strLocation could be easily switched between ‘Home’ or ‘Work’
Viewing a whole record
To view the whole record returned from
mysql_query(), we need another
function...
• resourceRecords – resource identifier returned from
mysql_query().
• it returns an array containing the database record.
array = mysql_fetch_row(resourceRecords)
Example: Displaying all customer
records
<?php
require_once("database2.php");
$dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
while ($arrRecord = mysql_fetch_row($dbRecords)) {
echo "<p>" . $arrRecord[0] . " ";
echo $arrRecord[1] . " ";
echo $arrRecord[2] . " ";
echo $arrRecord[3] . "</p>";
}
?>
• The function returns false when the last record is returned; thus, stopping
the loop.
• Note, however, that the fields are referred to by using numbers – not very
easy to read and mistakes can be introduced.
Limiting the records returned
SELECT Surname FROM customers
•Retrieves only the Surname field from the table customers
Limiting the records returned
SELECT * FROM customers LIMIT 3,4
• Select a certain number of records form a table
• 3 is the starting row
• 4 is the number of records to be selected after the starting
row
Searching for matching records
SELECT * FROM customers WHERE Title=‘Mr’
•The WHERE attribute specifies what to search for within the
database records.
• in this example, only records which have a title of ‘Mr’ will be
returned.
Searching for matching records
SELECT * FROM customers WHERE Title=‘Mr’
OR Title=‘Mrs’
•The WHERE attribute specifies what to search for within the
database records.
• in this example, only records which have a title of ‘Mr’ or
‘Mrs’ will be returned.
• we can also use AND and OR to formulate more
sophisticated conditions.
•
Searching for matching records
SELECT * FROM customers WHERE Title=‘Mr’
AND Surname=‘Smith’ OR Title=‘Mrs’
•The WHERE attribute specifies what to search for within the
database records.
• in this example, only records which have a surname of ‘Smith
and title of ‘Mr’ or the title of ‘Mrs’ will be returned.
• we can also use AND and OR to formulate more
sophisticated conditions.
•
Sorting records
The ORDER BY attribute can be used to sort
the order in which records are obtained.
• the ORDER BY attribute is followed by the data field on
which to sort the record
• DESC or ASC – from high to low, or from low to high
SELECT * FROM cutomers ORDER BY Surname DESC
Accessing Multiple Tables
<?php
// File: example15-13.php
require_once("database2.php");
$dbRecords = mysql_query("SELECT * FROM customers WHERE Title = 'Mrs'", $dbLocalhost)
or die("Problem reading table: " . mysql_error());
echo "<p>Customers:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords["Title"] . " ";
echo $arrRecords["Surname"] . " ";
echo $arrRecords["Firstname"] . "</p>";
}
//...continued...
Accessing Multiple Tables
//continuation...
$dbRecords = mysql_query("SELECT * FROM products WHERE Name = 'Wine Glass'",
$dbLocalhost)
or die("Problem reading table: " . mysql_error());
echo "<p>Products:</p>";
while ($arrRecords = mysql_fetch_array($dbRecords)) {
echo "<p>" . $arrRecords["Id"] . " ";
echo $arrRecords["Name"] . " ";
echo $arrRecords["Description"] . " ";
echo $arrRecords["Quantity"] . " ";
echo $arrRecords["Cost"] . "</p>";
}
?>

More Related Content

What's hot (20)

PPTX
Introduction to-python
Aakashdata
 
PDF
Nested Queries Lecture
Felipe Costa
 
PPSX
Php and MySQL
Tiji Thomas
 
PPT
PL/SQL Introduction and Concepts
Bharat Kalia
 
PPTX
Css selectors
Parth Trivedi
 
PPT
PYTHON - TKINTER - GUI - PART 1.ppt
PriyaSoundararajan1
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
Packages - PL/SQL
Esmita Gupta
 
PPT
Chapter 02 php basic syntax
Dhani Ahmad
 
PPTX
Sql and Sql commands
Knowledge Center Computer
 
PPTX
MYSQL join
Ahmed Farag
 
PDF
PL/SQL TRIGGERS
Lakshman Basnet
 
PPTX
python conditional statement.pptx
Dolchandra
 
PPTX
XML Document Object Model (DOM)
BOSS Webtech
 
PPTX
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
PDF
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
PPTX
DDL And DML
pnp @in
 
Introduction to-python
Aakashdata
 
Nested Queries Lecture
Felipe Costa
 
Php and MySQL
Tiji Thomas
 
PL/SQL Introduction and Concepts
Bharat Kalia
 
Css selectors
Parth Trivedi
 
PYTHON - TKINTER - GUI - PART 1.ppt
PriyaSoundararajan1
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
Packages - PL/SQL
Esmita Gupta
 
Chapter 02 php basic syntax
Dhani Ahmad
 
Sql and Sql commands
Knowledge Center Computer
 
MYSQL join
Ahmed Farag
 
PL/SQL TRIGGERS
Lakshman Basnet
 
python conditional statement.pptx
Dolchandra
 
XML Document Object Model (DOM)
BOSS Webtech
 
Introduction to HTML+CSS+Javascript.pptx
KADAMBARIPUROHIT
 
Chapter 4 Structured Query Language
Eddyzulham Mahluzydde
 
DDL And DML
pnp @in
 

Similar to PHP and MySQL.pptx (20)

PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
PPT
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
PPT
qwe.ppt
Heru762601
 
PDF
PHP and Mysql
Sankhadeep Roy
 
PPTX
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
PDF
PHP with MySQL
wahidullah mudaser
 
PPTX
Php basics
Egerton University
 
PPT
My sql with querys
NIRMAL FELIX
 
PDF
Php 2
tnngo2
 
PPT
Download It
webhostingguy
 
PPTX
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT V (5).pptx
DrDhivyaaCRAssistant
 
PPTX
Database Basics and MySQL
Jerome Locson
 
PPTX
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
PPSX
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
PPT
Mysql database
Arshikhan08
 
PPT
PHP - Getting good with MySQL part II
Firdaus Adib
 
PPTX
chapter_Seven Database manipulation using php.pptx
Getawu
 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
Lecture 15 - MySQL- PHP 1.ppt
TempMail233488
 
qwe.ppt
Heru762601
 
PHP and Mysql
Sankhadeep Roy
 
CHAPTER six DataBase Driven Websites.pptx
KelemAlebachew
 
PHP with MySQL
wahidullah mudaser
 
Php basics
Egerton University
 
My sql with querys
NIRMAL FELIX
 
Php 2
tnngo2
 
Download It
webhostingguy
 
Database Connectivity MYSQL by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT V (5).pptx
DrDhivyaaCRAssistant
 
Database Basics and MySQL
Jerome Locson
 
3-Chapter-Edit.pptx debre tabour university
alemunuruhak9
 
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
Mysql database
Arshikhan08
 
PHP - Getting good with MySQL part II
Firdaus Adib
 
chapter_Seven Database manipulation using php.pptx
Getawu
 
Ad

Recently uploaded (20)

PPTX
How to Manage Promotions in Odoo 18 Sales
Celine George
 
DOCX
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PPTX
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
How to Manage Promotions in Odoo 18 Sales
Celine George
 
A summary of SPRING SILKWORMS by Mao Dun.docx
maryjosie1
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
Federal dollars withheld by district, charter, grant recipient
Mebane Rash
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
community health nursing question paper 2.pdf
Prince kumar
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
LAW OF CONTRACT ( 5 YEAR LLB & UNITARY LLB)- MODULE-3 - LEARN THROUGH PICTURE
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
BÀI TẬP BỔ TRỢ THEO LESSON TIẾNG ANH - I-LEARN SMART WORLD 7 - CẢ NĂM - CÓ ĐÁ...
Nguyen Thanh Tu Collection
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
Pyhton with Mysql to perform CRUD operations.pptx
Ramakrishna Reddy Bijjam
 
Ad

PHP and MySQL.pptx

  • 2. Building Data Dynamic Web Sites  Truly dynamic web sites  Content changes over time  Content customised for individual user  Content automatically generated  Content Programmatically generated  Can be File system based  HTML and Images stored on File System  Gets hard to manage over time  Database based  HTML, Images etc all generated from database  Easier to manage  If data is too large, can overload the database
  • 4. Database • Structured collection of data. • Tables • Fields • Query • Reports • Essentially a much more sophisticated implementation of the flat files.
  • 6. Relational Database • Stores data in separate tables instead of a single store. • Relationships between tables are set • In theory, this provides a faster, more flexible database system.
  • 7. Example • We wish to maintain a database of student names, IDs, addresses, and any other information. • Will be updated frequently with new names and information. • Will want to retrieve data based on some predicate. • e.g, ‘give me the names of all Massey students who live in Albany’. • Will want to update database with new information about students, not previously recorded. • e.g., may decide we want to include IRD nos. • Very difficult to manage using ‘flat file’ systems
  • 8. Databases  Fast, Efficient back end storage  Easier to manage than file system based approach  Relational Database structure  Well developed theory and practise  Multi-user capable  Multithreaded, multiprocessor, sometimes cluster based systems  Standards based queries  Structured Query Language (SQL)
  • 9. MySQL Database  world's most popular open source database because of its consistent fast performance, high reliability and ease of use  Open Source License:- free  GNU General Public License  Free to modify and distribute but all modification must be available in source code format  Commercial:- not free  Fully paid up professional support • used by Google, Facebook Nokia, YouTube, Yahoo!, Alcatel-Lucent, Zappos.com, etc.
  • 10. Basic Database Server Concepts  Database runs as a server  Attaches to either a default port or an administrator specified port  Clients connect to database  For secure systems  authenticated connections  usernames and passwords  Clients make queries on the database  Retrieve content  Insert content  SQL (Structured Query Language) is the language used to insert and retrieve content
  • 11. • MySQL can be controlled through a simple command-line interface; however, we can use phpMyAdmin as an interface to MySQL. • phpMyAdmin is a very powerful tool; it provides a large number of facilities for customising a database management system.
  • 15. Specify the Table’s Fields & Attributes: Customers
  • 16. Table Edit Screen: Customers
  • 21. Connecting to a MySQL DBMS • In order for our PHP script to access a database we need to form a connection from the script to the database management system. resourceId = mysql_connect(server, username, password); • Server is the DBMS server • username is your username • password is your password
  • 22. Connecting to a MySQL DBMS • In order for our PHP script to access a database we need to form a connection from the script to the database management system. resourceId = mysql_connect(server, username, password); • The function returns a resource-identifier type. • a PHP script can connect to a DBMS anywhere in the world, so long as it is connected to the internet. • we can also connect to multiple DBMS at the same time.
  • 23. Selecting a database • Once connected to a DBMS, we can select a database. mysql_select_db(databasename, resourceId); • the resourceId is the one returned by mysql_connect() • the function returns true if the selection succeeded; false, otherwise.
  • 24. Example: Connect to a DBMS and access database <?php $dbLocalhost = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_select_db("glassesrus", $dbLocalhost) or die("Could not find database: " . mysql_error()); echo "<h1>Connected To Database</h1>"; ?> • die() stops execution of script if the database connection attempt failed. • mysql_error() returns an error message from the previous MYSQL operation.
  • 25. Reading from a database • We can now send an SQL query to the database to retrieve some data records. resourceRecords = mysql_query(query, resourceId); • the resourceId is the one returned by mysql_connect() • the function returns a resource identifier to the returned data.
  • 26. Example: Connect to a DBMS, access database, send query <?php $dbLocalhost = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_select_db("glassesrus", $dbLocalhost) or die("Could not find database: " . mysql_error()); $dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost) or die("Problem reading table: " . mysql_error()); echo "<h1>Connected To Database</h1>"; ?> • the function will return a resource pointer (not the actual data) to all the records that match the query. • If all goes well, this script will output nothing on screen.
  • 27. Extract contents of one record • We can now extract the actual data from the resource pointer returned by mysql_query(). fieldData= mysql_result(resourceRecords, row, field); • the resourceRecords is the one returned by mysql_query() • field – database field to return • the function returns the data stored in the field.
  • 28. Example: Connect to a DBMS, access database, send query <?php $dbLocalhost = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_select_db("glassesrus", $dbLocalhost) or die("Could not find database: " . mysql_error()); $dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost) or die("Problem reading table: " . mysql_error()); $strSurname = mysql_result($dbRecords, 0, "Surname"); echo "<p>$strSurname</p>"; ?> • the function will return a resource pointer (not the actual data) to all the records that match the query. • If all goes well, this script will output a surname on screen.
  • 29. SQL statement SELECT * FROM customers • Go and obtain from the database • every field • FROM the • customers table
  • 30. Separating the database connection It is worth separating the database connectivity from our scripts and placing it in a separate file. •It provides a convenient means of moving your scripts from one database platform to another.
  • 31. Example: Separating the database connection <?php // File: database2.php $strLocation = "Home"; //$strLocation = "Work"; if ($strLocation == "Home") { $dbLocalhost = mysql_connect("localhost", "root", "") or die("Could not connect: " . mysql_error()); mysql_select_db("glassesrus", $dbLocalhost) or die("Could not find database: " . mysql_error()); } else { $dbLocalhost = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error()); mysql_select_db("anotherdatabase", $dbLocalhost) or die("Could not find database: " . mysql_error()); } ?> • $strLocation could be easily switched between ‘Home’ or ‘Work’
  • 32. Viewing a whole record To view the whole record returned from mysql_query(), we need another function... • resourceRecords – resource identifier returned from mysql_query(). • it returns an array containing the database record. array = mysql_fetch_row(resourceRecords)
  • 33. Example: Displaying all customer records <?php require_once("database2.php"); $dbRecords = mysql_query("SELECT * FROM customers", $dbLocalhost) or die("Problem reading table: " . mysql_error()); while ($arrRecord = mysql_fetch_row($dbRecords)) { echo "<p>" . $arrRecord[0] . " "; echo $arrRecord[1] . " "; echo $arrRecord[2] . " "; echo $arrRecord[3] . "</p>"; } ?> • The function returns false when the last record is returned; thus, stopping the loop. • Note, however, that the fields are referred to by using numbers – not very easy to read and mistakes can be introduced.
  • 34. Limiting the records returned SELECT Surname FROM customers •Retrieves only the Surname field from the table customers
  • 35. Limiting the records returned SELECT * FROM customers LIMIT 3,4 • Select a certain number of records form a table • 3 is the starting row • 4 is the number of records to be selected after the starting row
  • 36. Searching for matching records SELECT * FROM customers WHERE Title=‘Mr’ •The WHERE attribute specifies what to search for within the database records. • in this example, only records which have a title of ‘Mr’ will be returned.
  • 37. Searching for matching records SELECT * FROM customers WHERE Title=‘Mr’ OR Title=‘Mrs’ •The WHERE attribute specifies what to search for within the database records. • in this example, only records which have a title of ‘Mr’ or ‘Mrs’ will be returned. • we can also use AND and OR to formulate more sophisticated conditions. •
  • 38. Searching for matching records SELECT * FROM customers WHERE Title=‘Mr’ AND Surname=‘Smith’ OR Title=‘Mrs’ •The WHERE attribute specifies what to search for within the database records. • in this example, only records which have a surname of ‘Smith and title of ‘Mr’ or the title of ‘Mrs’ will be returned. • we can also use AND and OR to formulate more sophisticated conditions. •
  • 39. Sorting records The ORDER BY attribute can be used to sort the order in which records are obtained. • the ORDER BY attribute is followed by the data field on which to sort the record • DESC or ASC – from high to low, or from low to high SELECT * FROM cutomers ORDER BY Surname DESC
  • 40. Accessing Multiple Tables <?php // File: example15-13.php require_once("database2.php"); $dbRecords = mysql_query("SELECT * FROM customers WHERE Title = 'Mrs'", $dbLocalhost) or die("Problem reading table: " . mysql_error()); echo "<p>Customers:</p>"; while ($arrRecords = mysql_fetch_array($dbRecords)) { echo "<p>" . $arrRecords["Id"] . " "; echo $arrRecords["Title"] . " "; echo $arrRecords["Surname"] . " "; echo $arrRecords["Firstname"] . "</p>"; } //...continued...
  • 41. Accessing Multiple Tables //continuation... $dbRecords = mysql_query("SELECT * FROM products WHERE Name = 'Wine Glass'", $dbLocalhost) or die("Problem reading table: " . mysql_error()); echo "<p>Products:</p>"; while ($arrRecords = mysql_fetch_array($dbRecords)) { echo "<p>" . $arrRecords["Id"] . " "; echo $arrRecords["Name"] . " "; echo $arrRecords["Description"] . " "; echo $arrRecords["Quantity"] . " "; echo $arrRecords["Cost"] . "</p>"; } ?>