Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Just to check that everything went well, Figure 17-1 shows the output you would get if
you selected everything from the catalog table from within the MySQL client. I got
this output by typing


SELECT * FROM catalog;


in the MySQL client.


Figure 17-1. SELECT * FROM catalog

The last step is to write a PHP script that gets the contents of the table and dresses it up in
an HTML table. Listing 17.3 lists PHP code for extracting the name and price values,
displaying them in an HTML table. The first step in communicating with a database
server is to connect to it. This is done with the mysql_pconnect function. It takes a
hostname, a username, and a password. I usually create a user named httpd in my
MySQL databases with no password. I also restrict this user to connections made from
the local server. I name it after the UNIX user who will be executing the scripts—in other
words, the Web server. If you are renting space from a hosting service, you may have a
MySQL user and database assigned to you, in which case you'll need to modify the
function arguments, of course.


Listing 17.3 Creating HTML Table from a Query

Free download pdf