Note that (Zeus web server) there must be no spaces after
Friday, April 20th, 2007Note that there must be no spaces after the =<<
Note that there must be no spaces after the =<<
Then you simply echo the contents of $table. And finally, you close the PHP script using the ?> tag. By using these two tags, you can use raw HTML code (that is, HTML code that does not need any modification at all). As you may recall from Chapter 2 in the discussion regarding using heredoc, you can change the text EOD to whatever you d like, but the beginning and ending tags must match. For example, this will work fine: $table =<<
EOD; echo $movie; ?> 2. Save this file as table1.php and upload it to your Web server. 3. Load your favorite browser and view the page that you have just uploaded. Your table should look like the one shown in Figure 4-1. How It Works All the code between <<
4 Using Tables to Display Data So now that you can successfully marry PHP and MySQL to produce dynamic pages, what happens when you have rows and rows of data that you need to display? You need to have some mechanism for your viewers to easily read the data, and it needs to be in a nice, neat, organized fashion. The easiest way to do this is to use tables. This chapter covers the following: . Creating a table to hold the data from the database . Creating column headings automatically . Populating the table with the results of a basic MySQL query . Populating the table with the results of more complex MySQL queries . Making the output user-friendly Creating a Table Before you can list your data, you need to set up the structure, column headings, and format of your table. Try It Out Defining the Table Headings In this exercise, you ll define the table headings for your table. 1. Open your favorite text/HTML editor and enter the following code:
| Movie Title |
|---|
| n ; echo $value; echo |
n ; ?> 2. Save the script and run it. How It Works You should see the same result as in the previous example. As you can see, you simply listed all the fields you wanted to see, regardless of the table they were in. (MySQL will find them as long as the table name is referenced there somewhere.) You did this in the first line of the SELECT statement: SELECT movie_name, movietype_label Then you told MySQL what tables you wanted to access and what type of join should be used to bring them together in these statements: FROM movie LEFT JOIN movietype 108 Chapter 3
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services
Figure 3-3 How It Works Now you can see a table with the movie names and actual words for the type of movie instead of your cryptic code, as was the case in Figure 3-2. The common fields were linked in the WHERE portion of the statement. ID numbers from the two different tables (fieldname movie_type in the movie table and fieldname movietype_id in the movietype table) represented the same thing, so that s where you linked them together. Joining Two Tables In life as in code, regardless of the circumstances under which two things join together, it is rarely a simple thing, and more often than not, it comes with conditions and consequences. In the world of MySQL, joins are also complex things that we discuss in greater detail in Chapter 10; meanwhile, we walk you through a very simple and commonly used join so you can get a taste of what joining is all about. The JOIN function gives you greater control over how your database tables relate to and connect with each other, but it also requires a greater understanding of relational databases (another topic covered in Chapter 10). 107 Using PHP5 with MySQL
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services
If a customer s ID is 123, you will see all the order_totals for all the orders for that specific customer, enabling you to determine all the money customer 123 has spent at your store. Although you are linking the two tables through the cust_ID field, the names do not have to be the same. You can compare any two field names from any two tables. An example would be: $query = SELECT customers.name, orders.order_total FROM customers, orders WHERE customers.email = orders.shiptoemail ; //retrieves customers names from customers table and order_total from //orders table where the email field in the customers table equals the //shiptoemail field in the orders table. This would link your tables through the email and shiptoemail fields from different tables. Try It Out Referencing Individual Tables This exercise will show you how to reference multiple tables in your query. 1. Change your select2.php program as shown here (changes are highlighted): 1990 . ORDER BY movie_type ; $results = mysql_query($query) or die(mysql_error()); echo
| n ; echo $value; echo |
n ; ?> 2. Save your script and run it. Your screen should look something like Figure 3-3. 106 Chapter 3
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web design programs services
Figure 3-2 You can get information from more than one table in two ways: . Reference the individual tables in your query and link them temporarily through a common field . Formally JOIN the individual tables in your query Let s try out these methods and then talk about each of them in more detail. Referencing Two Tables You can distinguish between two tables in your database by referencing them in the SELECT statement as follows: $query = SELECT customers.name, orders.order_total FROM customers, orders WHERE customers.cust_ID = orders.cust_ID ; //retrieves customers names from customers table and order_total from //orders table where the cust_ID field in the customers table equals the //cust_ID field in the orders table. 105 Using PHP5 with MySQL
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services