Download PHP and MySQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Microsoft SQL Server wikipedia , lookup

Concurrency control wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Relational model wikipedia , lookup

Clusterpoint wikipedia , lookup

Database model wikipedia , lookup

Transcript
PHP and MySQL
1
PHP and MySQL
Topics
 Querying Data with PHP
 User-Driven Querying
 Writing Data with PHP and MySQL
2
PHP and MySQL
Querying Data with PHP




3
Opening and Using a Database
Error Handling of MySQL Database Functions
Formatting Results
Using Include Files
PHP and MySQL
Opening and Using a Database



In PHP, there is no consolidated interface.
A set of library functions are provided for
executing SQL statements, as well as for
managing result sets returned from queries,
error handling, and setting efficiency options.
Connecting to and querying a MySQL DBMS
with PHP is a five-step process:
1. Connect to the DBMS and use a database.
» Open a connection to the MySQL DBMS using
mysql_connect( ).
4
PHP and MySQL
Opening and Using a Database
» There are three parameters:
The hostname of the DBMS server to use
 A username
 A password
» Once you connect, you can select a database to use
through the connection with the mysql_select_db( )
function.
» The function mysql_connect( ) returns a connection
handle.
» A handle is a value that can be used to access the
information associated with the connection.
5
PHP and MySQL
Opening and Using a Database
2. Run the query using mysql_query( ).
» The function takes two parameters:
 The SQL query itself
 The DBMS connection to use
 The connection parameter is the value returned
from the connection in the first step.
» The function mysql_query( ) returns a result set
handle resource; that is, a value that can retrieve the
output—the result set—of the query in Step 3.
6
PHP and MySQL
Opening and Using a Database
3. Retrieve a row of results.
» The function mysql_fetch_row( ) retrieves one row of
the result set.
» The function takes only one parameter:
 The result set handle from the second step
» Each row is stored in an array $row, and the attribute
values in the array are extracted in Step 4.
» A while loop is used to retrieve rows until there are
no more rows to fetch.
» The function mysql_fetch_row( ) returns false when
no more data is available.
7
PHP and MySQL
Opening and Using a Database
4. Process the attribute values.
» For each retrieved row, a for loop is used to print
with an echo statement each of the attributes in the
current row.
» Use mysql_num_fields( ) is used to return the
number of attributes in the row; that is, the number
of elements in the array.
» The function takes only one parameter:
 The result set handle from the second step
» The data itself is stored as elements of the array
$row returned in Step 3.
8
PHP and MySQL
Opening and Using a Database
5. Close the DBMS connection using mysql_close( )
»
The function takes only one parameter:
 The connection to be closed.
Example 4-1
Example 4-2
Example 4-3
Example 4-4
9
PHP and MySQL
Error Handling of MySQL Database Functions
 Database functions can fail.
 There are several possible classes of failure,
ranging from critical—the DBMS is inaccessible
or a fixed parameter is incorrect to recoverable,
such as a password being entered incorrectly by
the user.
Example 4-5
10
PHP and MySQL
Formatting Results
 Basic techniques for connecting to and querying
a MySQL DBMS using PHP can be extended to
produce results with embedded HTML that have
both better structure and presentation.
Example 4-6
11
PHP and MySQL
Using Include Files
 Include directives allow common functions in
other files to be accessible from within the body
of a script without directly adding the functions to
the code.
Example 4-7
Example 4-8
12
PHP and MySQL
User-Driven Querying
 User Input
 Passing Data with URLs
 Passing Data with the HTML <form> Environment
 Passing Data with Embedded Links
 How PHP Initializes Variables
 Querying with User Input
 Combined Scripts
13
PHP and MySQL
User Input
 Three techniques can be used to pass data that
drives the querying process in a web database
application:
 Manual entry of a URL to retrieve a PHP script
resource and provide parameters to the resource.
» For example, a user may open a URL using the Open Page
option in the File menu of the Netscape web browser.
 Data entry through HTML <form> environments.
» For example, <form> environments can capture textual
input, and input is made by selecting radio buttons, selecting
one or more items from a drop-down select list, clicking on
buttons, and through other data entry widgets.
14
PHP and MySQL
User Input
 Embedded hypertext links that can be clicked to
retrieve a PHP script resource and provide
parameters to the script.
15
PHP and MySQL
Passing Data with URLs
 Before the script is processed by the PHP
scripting engine, variables associated with any
parameters to the resource are initialized and
assigned values.
Example 5-1
16
PHP and MySQL
Passing Data with the HTML <form> Environment
 The second technique that captures data passed
from a browser to a server is the HTML <form>
environment.
Example 5-2
17
PHP and MySQL
Passing Data with Embedded Links
 The third technique that passes data from a web
browser to a web server is embedding links in an
HTML document.
 This technique runs queries in most web
database applications and is conceptually similar
to manually entering a URL.
Example 5-3
18
PHP and MySQL
How PHP Initializes Variables
 When the PHP script engine is invoked, the
engine declares and initializes variables in a
predefined order.
 The automatic initialization feature works in this
order:
1. By default, environment variables are initialized
first.
2. Variables are initialized from query string
parameters passed with the GET method.
3. POST method parameters are initialized.
4. Variables from cookies are initialized.
19
PHP and MySQL
How PHP Initializes Variables
5. The Apache server internal variables are
initialized.

20
The initialization order can be changed from
the default by adjusting the variables_order
setting in php.ini.
PHP and MySQL
Querying with User Input
 To introduce querying with user input, we begin
by explaining a script that retrieves the wines
made in a wine region that is specified by a user.
Example 5-5.
21
PHP and MySQL
Combined Scripts
 Some approaches separates the HTML <form>
and the PHP processing script into two files.
 It is more common to implement both in the
same script where the code can produce a
<form> or run a query, depending if user
parameters are supplied.
 If the script is called with no parameters, the
script produces a <form> for user input and, if it
is called with input from the <form>, it runs the
query. This is called a combined script.
Example 5-6.
22
PHP and MySQL
Writing Data with PHP and MySQL
 Database Inserts, Updates, and Deletes
 Uploading and Inserting Files into Databases
 Updating data
 Deleting data
23
PHP and MySQL
Database Inserts, Updates, and Deletes
 Simple database insertions and updates are
much the same as queries.
 Inserting, updating, and deleting data does
require some additional care.
Example 6-1
24
PHP and MySQL
Inserting data
 Phase one of the insertion process is data entry.
Example 6-5
 The second phase of insertion is data validation
and then the database operation itself.
Example 6-6
25
PHP and MySQL
Updating data
 Updating data is usually a more complex
process than inserting it. A three-step process
for updates is used in most web database
applications:
 Using a key value, matching data is read from the
database.
 The data is presented to the user for modification.
 The data is updated by writing the modified data
to the database, using the key value from the first
step.
Example 6-7
Example 6-8
26
PHP and MySQL
Deleting data
 The basic principle of deletion is a two-step
process:
 Identify the row or rows to be deleted
 Remove the data with an SQL DELETE
statement.
 As in an update, the first step requires a key
value be provided, and any technique described
for capturing keys in updates can be used.
 We assume here that a unique, primary key
value for the row to be deleted is available.
Example 6-8-1
27