Databases Reference
In-Depth Information
Figure 13-4. A form that requests the script printuser.php
</form>
</body>
</html>
When this HTML document is rendered by a web browser, as shown in Figure 13-4,
the user is able to enter their name into an input widget. Below the widget is a button
labeled “Show it!”. When the user presses the button, the script listed as the
action
attribute of the form element is requested, and the data in the input widget is sent to
the server as part of the URL.
For example, if the user enters the name
Selina
into the input widget and presses the
“Show it!” button, then the URL
http://localhost/printuser.php?username=Selina
is re-
quested. The web server runs the script
printuser.php
and passes the submitted data to
it. This script takes the submitted name and uses the
echo
statement to generate the
message “Hello,” followed by the text typed into the form. The web server returns any
output produced by the PHP script to the user's browser, which then displays it. In our
example, the user would see the message:
Hello, Selina
Example: Displaying the Artists from the Music Collection
We now look at another example that illustrates how you can use a PHP script, which
is accessible with a web browser, to run a query on a MySQL server and return a list of
artists from the
music
database; the script then formats this list as an HTML table for
display on the browser. We don't discuss the details of the process here, or the syntax
of the language; these are covered in the next two chapters. The aim of this section is
to give you a taste for how PHP and the web environment fit together with MySQL to
build a web database application.
You used the MySQL monitor in Chapter 5 to run basic queries on the
music
database.
Using it, you know how to display all artists:
mysql>
USE music;
Database changed
mysql>
SELECT * FROM artist;