PDF (Ebook) Learning PHP, MySQL & JavaScript (Early Release) by Robin Nixon ISBN 9781492093817, 1492093815 download
PDF (Ebook) Learning PHP, MySQL & JavaScript (Early Release) by Robin Nixon ISBN 9781492093817, 1492093815 download
com
https://ebooknice.com/product/learning-php-mysql-javascript-
early-release-24129590
DOWLOAD EBOOK
https://ebooknice.com/product/learning-php-mysql-javascript-4761774
ebooknice.com
ebooknice.com
https://ebooknice.com/product/learning-php-mysql-and-
javascript-1216936
ebooknice.com
https://ebooknice.com/product/learning-php-mysql-javascript-and-
css-2618790
ebooknice.com
(Ebook) Learning PHP, MySQL & JavaScript - With jQuery,
CSS & HTML5 by Robin Nixon ISBN 9781491978917, 1491978910
https://ebooknice.com/product/learning-php-mysql-javascript-with-
jquery-css-html5-7294746
ebooknice.com
ebooknice.com
ebooknice.com
ebooknice.com
Learning PHP, MySQL &
JavaScript
SIXTH EDITION
With Early Release ebooks, you get books in their earliest form—the
author’s raw and unedited content as they write—so you can take
advantage of these technologies long before the official release of these
titles.
Robin Nixon
Learning PHP, MySQL & JavaScript
by Robin Nixon
Copyright © 2021 Robin Nixon. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North,
Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales
promotional use. Online editions are also available for most titles
(http://oreilly.com). For more information, contact our
corporate/institutional sales department: 800-998-9938 or
[email protected].
Copyeditor: TK
Proofreader: TK
Indexer: TK
Audience
This book is for people who wish to learn how to create effective and
dynamic websites. This may include webmasters or graphic designers who
are already creating static websites but wish to take their skills to the next
level, as well as high school and college students, recent graduates, and
self-taught individuals.
In fact, anyone ready to learn the fundamentals behind responsive web
design will obtain a thorough grounding in the core technologies of PHP,
MySQL, JavaScript, CSS, and HTML5, and you’ll learn the basics of the
React library and React Native Framework, too.
Assumptions This Book Makes
This book assumes that you have a basic understanding of HTML and can
at least put together a simple, static website, but does not assume that you
have any prior knowledge of PHP, MySQL, JavaScript, CSS, or HTML5—
although if you do, your progress through the book will be even quicker.
Supporting Books
Once you have learned to develop using PHP, MySQL, JavaScript, CSS,
and HTML5, you will be ready to take your skills to the next level using the
following O’Reilly reference books:
Constant width
NOTE
This element signifies a tip, suggestion, or general note.
WARNING
This element indicates a warning or caution.
NOTE
For more than 40 years, O’Reilly Media has provided technology and business training,
knowledge, and insight to help companies succeed.
Our unique network of experts and innovators share their knowledge and
expertise through books, articles, and our online learning platform.
O’Reilly’s online learning platform gives you on-demand access to live
training courses, in-depth learning paths, interactive coding environments,
and a vast collection of text and video from O’Reilly and 200+ other
publishers. For more information, visit http://oreilly.com.
How to Contact Us
Please address comments and questions concerning this book to the
publisher:
Sebastopol, CA 95472
We have a web page for this book, where we list errata, examples, and any
additional information. You can access this page at [[[ERRATA URL GOES
HERE]]].
To comment or ask technical questions about this book, send email to
[email protected].
For news and more information about our books and courses, see our
website at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
Acknowledgments
I would like to thank Senior Content Acquisitions Editor, Amanda Quinn,
Content Development Editor, Melissa Potter, and everyone who worked so
hard on this book, including ???, ??? & ??? for their comprehensive
technical reviews, ??? for overseeing production, ??? for copy editing, ???
for proofreading, ??? for creating the index, Karen Montgomery for the
original sugar glider front cover design, ??? for the latest book cover, my
original editor, Andy Oram, for overseeing the first five editions, and
everyone else too numerous to name who submitted errata and offered
suggestions for this new edition.
Chapter 1. Introduction to
Dynamic Web Content
The World Wide Web is a constantly evolving network that has already
traveled far beyond its conception in the early 1990s, when it was created to
solve a specific problem. State-of-the-art experiments at CERN (the
European Laboratory for Particle Physics, now best known as the operator
of the Large Hadron Collider) were producing incredible amounts of data—
so much that the data was proving unwieldy to distribute to the participating
scientists, who were spread out across the world.
At this time, the internet was already in place, connecting several hundred
thousand computers, so Tim Berners-Lee (a CERN fellow) devised a
method of navigating between them using a hyperlinking framework, which
came to be known as Hypertext Transfer Protocol, or HTTP. He also created
a markup language called Hypertext Markup Language, or HTML. To bring
these together, he wrote the first web browser and web server.
Today we take these tools for granted, but back then, the concept was
revolutionary. The most connectivity so far experienced by at-home modem
users was dialing up and connecting to a bulletin board that was hosted by a
single computer, where you could communicate and swap data only with
other users of that service. Consequently, you needed to be a member of
many bulletin board systems in order to effectively communicate
electronically with your colleagues and friends.
But Berners-Lee changed all that in one fell swoop, and by the mid-1990s,
there were three major graphical web browsers competing for the attention
of 5 million users. It soon became obvious, though, that something was
missing. Yes, pages of text and graphics with hyperlinks to take you to other
pages was a brilliant concept, but the results didn’t reflect the instantaneous
potential of computers and the internet to meet the particular needs of each
user with dynamically changing content. Using the web was a very dry and
plain experience, even if we did now have scrolling text and animated
GIFs!
Shopping carts, search engines, and social networks have clearly altered
how we use the web. In this chapter, we’ll take a brief look at the various
components that make up the web, and the software that helps make using it
a rich and dynamic experience.
NOTE
It is necessary to start using some acronyms more or less right away. I have tried to
clearly explain them before proceeding, but don’t worry too much about what they stand
for or what these names mean, because the details will become clear as you read on.
Although it’s helpful to be aware of this process so that you know how the
three elements work together, in practice you don’t really need to concern
yourself with these details, because they all happen automatically.
The HTML pages returned to the browser in each example may well
contain JavaScript, which will be interpreted locally by the client, and
which could initiate another request—the same way embedded objects such
as images would.
<?php
echo " Today is " . date("l") . ". ";
?>
The opening <?php tells the web server to allow the PHP program to
interpret all the following code up to the ?> tag. Outside of this construct,
everything is sent to the client as direct HTML. So, the text Here's the
latest news. is simply output to the browser; within the PHP tags, the
built-in date function displays the current day of the week according to
the server’s system time.
The final output of the two parts looks like this:
PHP is a flexible language, and some people prefer to place the PHP
construct directly next to PHP code, like this:
There are even more ways of formatting and outputting information, which
I’ll explain in the chapters on PHP. The point is that with PHP, web
developers have a scripting language that, although not as fast as compiling
your code in C or a similar language, is incredibly speedy and also
integrates seamlessly with HTML markup.
NOTE
If you intend to enter the PHP examples in this book into a program editor to work
along with me, you must remember to add <?php in front and ?> after them to ensure
that the PHP interpreter processes them. To facilitate this, you may wish to prepare a file
called example.php with those tags in place.
Using PHP, you have unlimited control over your web server. Whether you
need to modify HTML on the fly, process a credit card, add user details to a
database, or fetch information from a third-party website, you can do it all
from within the same PHP files in which the HTML itself resides.
Using MySQL
Of course, there’s not a lot of point to being able to change HTML output
dynamically unless you also have a means to track the information users
provide to your website as they use it. In the early days of the web, many
sites used “flat” text files to store data such as usernames and passwords.
But this approach could cause problems if the file wasn’t correctly locked
against corruption from multiple simultaneous accesses. Also, a flat file can
get only so big before it becomes unwieldy to manage—not to mention the
difficulty of trying to merge files and perform complex searches in any kind
of reasonable time.
That’s where relational databases with structured querying become
essential. And MySQL, being free to use and installed on vast numbers of
internet web servers, rises superbly to the occasion. It is a robust and
exceptionally fast database management system that uses English-like
commands.
The highest level of MySQL structure is a database, within which you can
have one or more tables that contain your data. For example, let’s suppose
you are working on a table called users, within which you have created
columns for surname, firstname, and email, and you now wish to
add another user. One command that you might use to do this is as follows:
INSERT INTO users VALUES('Smith', 'John', '[email protected]');
You will previously have issued other commands to create the database and
table and to set up all the correct fields, but the SQL INSERT command
here shows how simple it can be to add new data to a database. SQL is a
language designed in the early 1970s that is reminiscent of one of the oldest
programming languages, COBOL. It is well suited, however, to database
queries, which is why it is still in use after all this time.
It’s equally easy to look up data. Let’s assume that you have an email
address for a user and need to look up that person’s name. To do this, you
could issue a MySQL query such as the following:
MySQL will then return Smith, John and any other pairs of names that
may be associated with that email address in the database.
As you’d expect, there’s quite a bit more that you can do with MySQL than
just simple INSERT and SELECT commands. For example, you can
combine related data sets to bring related pieces of information together,
ask for results in a variety of orders, make partial matches when you know
only part of the string that you are searching for, return only the nth result,
and a lot more.
Using PHP, you can make all these calls directly to MySQL without having
to directly access the MySQL command-line interface yourself. This means
you can save the results in arrays for processing and perform multiple
lookups, each dependent on the results returned from earlier ones, to drill
down to the item of data you need.
For even more power, as you’ll see later, there are additional functions built
right into MySQL that you can call up to efficiently run common operations
within MySQL, rather than creating them out of multiple PHP calls to
MySQL.
Using JavaScript
The oldest of the three core technologies discussed in this book, JavaScript,
was created to enable scripting access to all the elements of an HTML
document. In other words, it provides a means for dynamic user interaction
such as checking email address validity in input forms and displaying
prompts such as “Did you really mean that?” (although it cannot be relied
upon for security, which should always be performed on the web server).
Combined with CSS (see the following section), JavaScript is the power
behind dynamic web pages that change in front of your eyes rather than
when a new page is returned by the server.
However, JavaScript can also be tricky to use, due to some major
differences in the ways different browser designers have chosen to
implement it. This mainly came about when some manufacturers tried to
put additional functionality into their browsers at the expense of
compatibility with their rivals.
Thankfully, the developers have mostly now come to their senses and have
realized the need for full compatibility with one another, so it is less
necessary these days to have to optimize your code for different browsers.
However, there remain millions of users using legacy browsers, and this
will likely be the case for a good many years to come. Luckily, there are
solutions for the incompatibility problems, and later in this book we’ll look
at libraries and techniques that enable you to safely ignore these differences.
For now, let’s take a look at how to use basic JavaScript, accepted by all
browsers:
<script type="text/javascript">
document.write("Today is " + Date() );
</script>
This code snippet tells the web browser to interpret everything within the
<script> tags as JavaScript, which the browser does by writing the text
Today is to the current document, along with the date, using the
JavaScript function Date. The result will look something like this:
NOTE
Unless you need to specify an exact version of JavaScript, you can normally omit the
type="text/javascript" and just use <script> to start the interpretation of
the JavaScript.
Using CSS
CSS is the crucial companion to HTML, ensuring that the HTML text and
embedded images are laid out consistently and in a manner appropriate for
the user’s screen. With the emergence of the CSS3 standard in recent years,
CSS now offers a level of dynamic interactivity previously supported only
by JavaScript. For example, not only can you style any HTML element to
change its dimensions, colors, borders, spacing, and so on, but now you can
also add animated transitions and transformations to your web pages, using
only a few lines of CSS.
Using CSS can be as simple as inserting a few rules between <style> and
</style> tags in the head of a web page, like this:
<style>
p {
text-align:justify;
font-family:Helvetica;
}
</style>
These rules change the default text alignment of the <p> tag so that
paragraphs contained in it are fully justified and use the Helvetica font.
As you’ll learn in Chapter 19 , there are many different ways you can lay
out CSS rules, and you can also include them directly within tags or save a
set of rules to an external file to be loaded in separately. This flexibility not
only lets you style your HTML precisely, but can also (for example)
provide built-in hover functionality to animate objects as the mouse passes
over them. You will also learn how to access all of an element’s CSS
properties from JavaScript as well as HTML.
NOTE
One of the little things I like about the HTML5 specification is that XHTML syntax is
no longer required for self-closing elements. In the past, you could display a line break
using the <br> element. Then, to ensure future compatibility with XHTML (the
planned replacement for HTML that never happened), this was changed to <br />, in
which a closing / character was added (since all elements were expected to include a
closing tag featuring this character). But now things have gone full circle, and you can
use either version of these types of elements. So, for the sake of brevity and fewer
keystrokes, in this book I have reverted to the former style of <br>, <hr>, and so on.
FOOTNOTES:
[113] I make frequent use of this word to mean an activity
which furthers some general interest of the social group. It differs
from “purpose” in not necessarily implying intention.
[114] Democracy and Social Ethics, 219.
CHAPTER XXII
HOW FAR WEALTH IS THE BASIS OF OPEN CLASSES
FOOTNOTES:
[115] Walden, 89, 91.
[116] The Varieties of Religious Experience, 368.
[117] I will not here discuss the question just how far it serves
a useful purpose in the economic system.
[118] If the reader cares to know my opinion of that doctrine—
sometimes called the economic interpretation of history—which
teaches that economic conditions are in a peculiar sense the
primary and determining factor in society, he will find it in the
following passages:
“The organic view of history [which I hold] denies that any
factor or factors are more ultimate than others. Indeed it denies
that the so-called factors—such as the mind, the various
institutions, the physical environment and so on—have any real
existence apart from a total life in which all share in the same
way that the members of the body share in the life of the animal
organism. It looks upon mind and matter, soil, climate, flora,
fauna, thought, language and institutions as aspects of a single
rounded whole, one total growth. We may concentrate attention
upon some one of these things, but this concentration should
never go so far as to overlook the subordination of each to the
whole, or to conceive one as precedent to others.”
“I cannot see that the getting of food, or whatever else the
economic activities may be defined to be, is any more the logical
basis of existence than the ideal activities. It is true that there
could be no ideas and institutions without a food supply; but no
more could we get food if we did not have ideas and institutions.
All work together, and each of the principal functions is essential
to every other.”
“History is not like a tangled skein which you may straighten
out by getting hold of the right end and following it with sufficient
persistence. It has no straightness, no merely lineal continuity, in
its nature. It is a living thing, to be known by sharing its life, very
much as you know a person. In the organic world—that is to say
in real life—each function is a centre from which causes radiate
and to which they converge; all is alike cause and effect; there is
no logical primacy, no independent variable, no place where the
thread begins. As in the fable of the belly and the members, each
is dependent upon all the others. You must see the whole or you
do not truly see anything.” (Publications of the American
Economic Association, Third Series, vol. v, 426 ff.)
CHAPTER XXIII
ON THE ASCENDENCY OF A CAPITALIST CLASS
“He lets every one remain just what he is, but takes care that
he shall always be it in the right place: thus he knows how to
make all men’s power his own.” Schiller, Wallenstein’s Lager, I, 4.
CHAPTER XXIV
ON THE ASCENDENCY OF A CAPITALIST CLASS—Continued
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebooknice.com