Database Reference
In-Depth Information
Except for the break tags,
show_tables.php
includes HTML content by writing it outside
of the
<?php
and
?>
tags so that the PHP interpreter simply writes it without interpre‐
tation. Here's a different version that produces all the HTML using
print
statements:
<?
php
# show_tables_print.php: Display names of tables in cookbook database
# using print() to generate all HTML
require_once
"Cookbook.php"
;
print
(
"<html>"
);
print
(
"<head><title>Tables in cookbook Database</title></head>"
);
print
(
"<body>"
);
print
(
"<p>Tables in cookbook database:</p>"
);
# Connect to database, display table list, disconnect
$dbh
=
Cookbook
::
connect
();
$stmt
=
"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'cookbook' ORDER BY TABLE_NAME"
;
$sth
=
$dbh
->
query
(
$stmt
);
while
(
list
(
$tbl_name
)
=
$sth
->
fetch
(
PDO
::
FETCH_NUM
))
print
(
$tbl_name
.
"<br />"
);
$dbh
=
NULL
;
print
(
"</body>"
);
print
(
"</html>"
);
?>
Sometimes it makes sense to use one approach, sometimes the other—and sometimes
both within the same script. If a section of HTML contains no references to variable or
expression values, it can be clearer to write it in HTML mode. Otherwise it may be
clearer to write it using
print
or
echo
statements, to avoid switching between HTML
and PHP modes frequently.
Python
A standard installation of Python includes
cgi
and
urllib
modules that are useful for
web programming. However, we don't actually need them yet because the only web-
related activity of our first Python web script is to generate some simple HTML. Here's
a Python version of the MySQL table-display script:
#!/usr/bin/python
# show_tables.py: Display names of tables in cookbook database
import
cookbook
# Print header, blank line, and initial part of page
print
(
'''Content-Type: text/html
<html>