PHP MySQL in Easy Steps 2nd Edition TOCCh1
PHP MySQL in Easy Steps 2nd Edition TOCCh1
Contents
1 Getting started 7
2 Performing operations 25
Creating variables 26
Managing strings 28
Producing arrays 30
Sorting arrays 32
Describing dimensions 34
Doing arithmetic 36
Comparing values 38
Assessing logic 40
Defining constants 42
Summary 44
3 Controlling progress 45
Branching alternatives 46
Switching branches 48
Performing loops 50
Looping while true 52
Breaking from loops 54
Creating functions 56
Passing arguments 58
Returning values 60
Summary 62
4 Producing forms 63
Performing actions 64
Checking set values 66
Validating form data 68
Sending hidden data 70
Handling submissions 72
Making sticky forms 74
Surrounding forms 76
Appending link data 78
Moving location 80
Summary 82
5 Assembling tables 83
Introducing tables 84
Creating tables 86
Defining data types 88
Adding modifiers 90
Setting primary keys 92
Altering tables 94
Summary 96
6 Handling data 97
Inserting data 98
Updating columns 100
Updating fields 102
Deleting data 104
Selecting data 106
Retrieving columns 108
Retrieving rows 110
Sorting data 112
Setting direction 114
Making comparisons 116
Summary 118
Index 187
Preface
The creation of this book has provided me, Mike McGrath, a welcome opportunity to
demonstrate the latest server-side scripting techniques with PHP and MySQL databases. All
examples I have given in this book demonstrate modern features of the PHP scripting language
using the current MySQL Relational Database Management System that is supported on both
Windows and Linux operating systems. I sincerely hope you enjoy discovering the exciting
possibilities of PHP and MySQL, and have as much fun with it as I did in writing this book.
In order to clarify the code listed in the steps given in each example, I have adopted certain
colorization conventions. Components of the PHP language are colored blue; programmer-
specified names are red; numeric and string data values are black; and comments are green:
<?php
# Write the traditional greeting.
$string = ‘<p>Hello World!</p>’ ;
echo $string ;
?>
Similarly, components of the SQL query language are colored blue; programmer-specified
names are red; numeric and string data values are black; and comments are green:
# Insert 5 records into the "top_5_films" table.
INSERT INTO top_5_films ( position , title , year ) VALUES ( 1 , "Citizen Kane" , 1941 ) ;
Additionally, in order to identify each source code file described in the steps, a colored icon and
file name appears in the margin alongside the steps:
SQL
For convenience I have placed source code files from the examples featured in this book into a
single ZIP archive. You can obtain the complete archive by following these easy steps:
l 2 Find PHP & MySQL in easy steps, 2nd edition in the list, then click on the hyperlink
entitled All Code Examples to download the archive
l 3 Now, extract the archive contents to any convenient location on your computer
1 Getting started
8 Introducing PHP & MySQL
Welcome to the exciting
9 Understanding The Cloud
world of the data-driven
10 Installing Abyss Web Server
web with PHP & MySQL.
12 Installing the PHP engine
This chapter demonstrates
14 Configuring Abyss for PHP
how to create a dynamic
16 Embedding PHP script
development environment
18 Installing the MySQL Server
incorporating the Abyss Web
20 Using the MySQL Client
Server, the PHP engine, and
21 Creating MySQL users
the MySQL database server.
22 Connecting PHP & MySQL
24 Summary
Getting started
What is PHP?
PHP is a widely-used general purpose scripting language that is
especially suited for web development, and can be embedded into
HTML. It was created by programmer Rasmus Lerdorf as a set of
scripts to maintain his website that he released as “Personal Home
Page Tools (PHP Tools) version 1.0” on June 8, 1995. These were
extended in the version 2 release of 1997, and the name changed
to become a recursive acronym “PHP: Hypertext Preprocessor”
in version 3 the following year. Performance, reliability, and
extensibility were improved in 2000 with the release of PHP4,
which was powered by the new Zend engine – a virtual machine.
The current version, PHP5, is powered by the Zend II engine
and produced as free software by the PHP group. Today, PHP is
installed on over 20 million websites and 1 million web servers.
What is MySQL?
8
Web Browser
HTTP (HyperText Transfer
Protocol) is the common
User Interface communication standard
that allows any computer
connected to any web
HTTP Response server to access files
HTTP Request across the web.
Connection to Domain
9
Web Server
HTML Response
PHP Request
Data MySQL
Server
The examples in this
book are created and
tested with the listed
The ensuing pages describe how to create a development
software versions but
environment for data-driven websites by installing the following may require modification
server-side technologies on your own computer: for other versions.
Further guidance on
installation of the Abyss
Web Server is available
at aprelium.com/
abyssws/start.html
10
l 2 Run the setup installer and accept the License terms, then
choose to install with the recommended options
l
at http://localhost:9999.
5 Now, log in using your chosen name and password to see
the Abyss console confirm the server status as Running
11
l6 Finally, enter http://localhost into your browser address
field, then hit Return to see the default Abyss Welcome
page get served up by the Abyss Web Server
Further guidance on
installation of PHP is
l 1 Download the PHP setup package for your system from
aprelium.com/downloads
available at php.net/
manual/en/install.php l 2 Run the downloaded executable file to launch the Setup
Wizard, then click on the Next button to begin
12
13
l5 Finally, after the installation completes, click on the
Finish button to close the Setup Wizard
Following installation
of PHP, the web server
cannot yet execute
PHP scripts until it is
configured to recognize
them and to find the
PHP interpreter engine –
all as described on
pages 14-15.
Getting started
Further guidance on
configuration of the
Abyss Web Server is
available online at
aprelium.com/abyssws/
l 2 Next, click on the Scripting Parameters icon to open
the Scripting Parameters page for editing
l
start.html
14
l 6 Open a plain text editor and exactly type the script below
<?php phpinfo() ?>
15
your web browser’s address field to see Abyss serve up a
web page containing your PHP version information
</body>
</html>
<?php
?>
17
l 6 Now, use your web browser’s View Source facility to see
that PHP has written the content into the body section,
including the HTML <h1></h1> heading tags
Further guidance on
installation of the MySQL
l
Server is available at
http://dev.mysql.com/ 2 Run the installer and accept the License terms, then
doc/refman/8.0/en/ select “Server only” and click Next to continue
installing.html
18
19
7 Enter a root user password of your choice twice into the
Password fields, then click Next to continue
Write down your chosen
root user password,
username, and user
password – you will
need them often.
l
end with a ; semi-colon.
3 Now, precisely issue a further command to display all
databases that now exist on your MySQL Server
SHOW DATABASES ;
Installation creates
some default databases,
such as “sys”, but your
“site_db” database is the
one that will be used
throughout this book.
Creating MySQL users
While the MySQL root user is allowed complete control over
the databases on the MySQL Server, it is obviously inadvisable to
allow other users such freedom for best security of the databases.
The root user can therefore create users with specific “privileges”
controlling what actions they may perform on the MySQL Server.
MySQL is case-sensitive
The root user can create a user in the MySQL Command Line and requires precise
Client by issuing a clause to identify a unique user, like this: syntax – you must be
sure to use correct
CREATE USER IF NOT EXISTS ‘username’@’hostname’ spacing and letter case
IDENTIFIED WITH mysql_native_password BY ‘password’ ; as it appears listed. For
example, do not leave
The root user can then issue a clause to specify privileges allowed
spaces around the @
for a particular database to a particular user, like this: character that defines
the user name and host.
GRANT privileges ON database.* TO ‘username’@‘hostname’ ;
21
user, then precisely issue this clause to create a user
CREATE USER IF NOT EXISTS ‘mike’@’locahost’
IDENTIFIED WITH mysql_native_password BY ‘easysteps’ ;
l
MySQL 8 introduces
2 Next, allow privileges to access the “site_db” database caching_sha2_password
GRANT SELECT, INSERT, UPDATE ON site_db.* authentication, but the
TO ‘mike’@‘localhost’ ; mysql_native_password
l
earlier authentication can
3 Now, issue a clause to confirm this user’s privileges still be used to specify
SHOW GRANTS FOR ‘mike’@’localhost’ ; unencrypted passwords.
Getting started
You need not understand in detail how the script works at this
stage, but recognize that it contains sensitive information. For
this reason it should not be placed in the web server’s /htdocs
directory like all other PHP scripts, where its contents may be
accessible, but placed instead safely in /htdocs parent directory
– for example, in the “C:/Abyss Web Server” directory rather
than in the “C:/Abyss Web Server/htdocs” directory.
Any PHP script can incorporate another PHP script by
using a “require” statement to specify the other script’s path.
22
23
l 6 Temporarily edit connect_db.php by changing the
database name to one that does not exist, then save the
file and reload the page in your browser to see the error
l
them later to see how
7 Finally, correct the connect_db.php script by changing your knowledge of PHP
has progressed.
the database name back to site_db, then save the file and
reload the page to see the connection succeed once more
Summary
• PHP is a scripting language that is especially suited for web
development, as it can be embedded in HTML.
• Aa Web
local development environment can be created by installing
Server, the PHP engine, and the MySQL Server.
• All tags
?>
embedded PHP code must be contained within <?php
so it can be readily recognized by the PHP engine.
and