Although you didn’t write any PHP in this chapter, you did learn how to create a basic data structure using the SQL language.. You learned how to work with the MySQL console to create an
Trang 1INSERT INTO adventure values(
13, ‘replace Enigma and wait’,’You put the Enigma back in place and wait
patiently, but you never get another chance You are discovered when the
sub pulls in to harbor.’, 19, 0, 0, 0
);
INSERT INTO adventure values(
14, ‘Win’, ‘Congratulations! You have captured the device and shortened
the war!’, 1, 0, 0, 0
);
INSERT INTO adventure values(
15, ‘Water’, ‘You are in the water The sub moves away It looks
bad ’, 19, 0, 0, 0
);
INSERT INTO adventure values(
16,’’,’’, 0, 0, 0, 0
);
INSERT INTO adventure values(
17,’’,’’, 0, 0, 0, 0
);
INSERT INTO adventure values(
18,’’,’’, 0, 0, 0, 0
);
INSERT INTO adventure values(
19, ‘Game Over’ ,’The game is over You lose.’, 1, 0, 0, 0
);
SELECT id, name, north, east, south, west FROM adventure;
SELECT id, description FROM adventure;
I wrote this code by hand, but I could have designed it with phpMyAdmin just as
easily Note that I created the table, inserted values, and wrote a couple of SELECT
statements to check the values I like to have a script for creating a database even
if I built it in a tool like phpMyAdmin, because I managed to mess up this
data-base several times as I was writing the code for this chapter It is very handy to
have a script that instantly rebuilds the database without any tears
333
i n
Trang 2Although you didn’t write any PHP in this chapter, you did learn how to create a basic data structure using the SQL language You learned how to work with the MySQL console to create and use databases and how to return data from your database using the SELECTstatement You know how to modify the SELECT state-ment to get more-specific results You know how phpMyAdmin can simplify the creation and manipulation of MySQL databases You built a data structure for an adventure game
334
l u
C H A L L E N G E S
1 Design a database Start with something simple like a phone list.
2 Create your database in SQL.
3 Write a batch program to create and populate your database.
4 Use phpMyAdmin to manipulate your database and view its results in other formats.
5 Read appendix B to see how SQLite is like (and unlike) MySQL Make
a basic table using SQLite.
Trang 3After all this talk of databases, you might be eager to connect a database to your PHP programs PHP is well known for its seamless database integration,
especially with MySQL It’s actually quite easy to connect to a MySQL database from within PHP Once you’ve established the connection, you can send SQL commands to the database and receive the results as data you can use in your PHP program
By the end of this chapter you will have built the adventure game featured at the beginning of chapter 9, “Using MySQL to Create Databases.” As you see, the pro-gramming isn’t very hard if the data is designed well Specifically, you learn how to:
• Get a connection to a MySQL database from within PHP.
• Use a particular database.
• Send a query to the database.
• Parse the query results.
• Check for data errors.
• Build HTML output from data results.
Connecting to
Databases within PHP
Trang 4Connecting to the Hero Database
To show how database connection works, I build a simple PHP program that returns all the values in the Hero database you created in chapter 9 Figure 10.1 illustrates the Show Hero PHP program
I decided to go back to this simpler database rather than the more complex adven-ture game When you’re learning new concepts, it’s best to work with the simplest environment at first and then move to more complex situations The adventure database has a lot of information in it, and the way the records point to each other
is complicated With a simpler database I was sure I understood the basics of data connection before working with a production database that is bound to have com-plexities of its own.
This is the code that generates this page:
<body>
<h1>Show Heros</h1>
H I N T
336
l u
FIGURE 10.1
This HTML table is
generated by a PHP
program reading
the database.
Trang 5//make the database connection
$conn = mysql_connect(“localhost”, “”, “”);
mysql_select_db(“chapter7”, $conn);
//create a query
$sql = “SELECT * FROM hero”;
$result = mysql_query($sql, $conn);
print “<table border = 1>\n”;
//get field names
print “<tr>\n”;
while ($field = mysql_fetch_field($result)){
print “ <th>$field->name</th>\n”;
} // end while
print “</tr>\n\n”;
//get row data as an associative array
while ($row = mysql_fetch_assoc($result)){
print “<tr>\n”;
//look at each field
foreach ($row as $col=>$val){
print “ <td>$val</td>\n”;
} // end foreach
print “</tr>\n\n”;
}// end while
print “</table>\n”;
?>
</body>
</html>
Glance over the code and you see it’s mostly familiar except for a few new functions
that begin with mysql_ These functions allow access to MySQL databases If you
look through the PHP documentation you see very similar functions for several
other types of databases, including Oracle, Informix, mSQL, and ODBC You’ll
find the process for connecting to and using other databases is pretty much the
same no matter which database you’re using
337
i t