Wrox Programmer Forums
|
BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0
This is the forum to discuss the Wrox book Beginning PHP5, Apache, and MySQL Web Development by Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz, Michael K. Glass; ISBN: 9780764579660
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 22nd, 2005, 04:17 PM
Authorized User
 
Join Date: Aug 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 10 code errors in the book

I have finished the Comic Book Appreciation exercise of Chapter 10 and have found a few mistakes in the code in the book:

#1
In charlist.php the code to select the character-enemies (good-bad or bad-good) combinations in order to display them on the home page is as follows:
$sql = "SELECT c.id, n.alias " .
        "FROM char_main c " .
        "JOIN char_good_bad_link gb " .
        "JOIN char_main n " .
        "ON (c.id = gb.good_id AND n.id = gb.bad_id) " .
        "ON (n.id = gb.good_id AND c.id = gb.bad_id)";

This query does not return any records from char_good_bad_links table even though there are records that shoud be returned. The home page has "none" for enemies in each row. I struggled with this using MySQLQueryBrowser to test various queries.

Fianally I found an article at "http://www.devx.com/dbzone/Article/17403/1954?pf=true" titled "Getting the Right Data with SQL Joins" by Susan Sales Harkins and Patricia E. O' Connor, This is what it said about 3 table joins"

"SELECT fldlist FROM table1 JOIN table2
  ON table1.primarykey = table2.foreignkey JOIN table3
  ON table2.primarykey = table3.foreignkey

The first join applies to table1 and table2, the second join applies to table2 and table3. You might hear table2 referred to as the intermediate or translation table because it's the connection table for the other two tables. In a really complex statement, you might have several intermediate tables. Continue to add joins in this manner until your statement includes all the relationships and tables necessary to get just the right results. This is another fairly generic behavior that may or may not be strictly enforced by your system; be sure to check your system's documentation." end of quote.

Based on this syntax I found that the following worked:
$sql = "SELECT c.id, n.alias " .
        "FROM char_main c " .
        "JOIN char_good_bad_link gb " .
        "ON ( c.id = gb.good_id OR c.id=gb.bad_id) " .
        "AND (c.id != n.id) " .
        "JOIN char_main n " .
        "ON (n.id = gb.bad_id OR n.id = gb.good_id)";

#2
In char_transact.php - the Delete Character switch path ,the following did not work:
$sql = "DELETE FROM char_main, char_lair " .
           "USING char_main m, char_lair l " .
           "WHERE m.lair_id = l.id AND m.id = $cid";

When I dropped the table aliases it worked (I don't understand why) - see below:
$sql = "DELETE FROM char_main, char_lair " .
       "USING char_main, char_lair " .
       "WHERE char_main.lair_id = char_lair.id " .
       "AND char_main.id = $cid";

#3
In charedit.php only one character was shown in the Enemies list on the Character Edit form regardless of the number of characters in the char_main table and the number of combinations in the char_good_bad_links table This problem also appeared in a post to this forum as a question by author Sagh on 7/18/2005 whch was never replied to.

$sql = "SELECT id, alias FROM char_main WHERE id != $char";
$result = mysql_query($sql)
  or die(mysql_error());
if (mysql_num_rows($result) > 0) {
  $row = mysql_fetch_array($result);
  $charlist[$row['id']] = $row['alias'];
}

The problem is that this fetches only the first row of the returned records. The corrected code which is similar to the $pwrlist code is as follows:
if (mysql_num_rows($result) > 0) {
   While($row = mysql_fetch_array($result)) {
       $charlist[$row['id']] = $row['alias'];
   }
}

#4
The code to build the character list that is used to select enemies is as follows:
<?php if (isset($charlist) && is_array($charlist)) { ?>
  <tr>
    <td>Enemies:<br>
      (Ctrl-click to<br>select multiple<br>enemies)
    </td>
    <td>
      <select multiple name="enemies[]" size="4">
<?php
foreach ($charlist as $key => $value) {
  echo "<option value=\"$key\" ";
  if (isset($enemies)) {
    echo $enemies[$key];
  }
  echo ">$value</option>\n";
}
?>

When the user comes back to charedit.php to change something the above foreach loop gets an undefined item error on each character not selected as an enemy. The foreach loop should be: (this is similar to the foreach power loop)

foreach ($charlist as $key => $value) {
  echo "<option value=\"$key\" ";
  if (isset($enemies) && array_key_exists($key,$enemies)) {
    echo $enemies[$key];
  }
  echo ">$value</option>\n";
}

I don't know wheather or not these errors are corrected in an Errata list. Maybe the authors left these errors in as an

exercise for the student to find and fix. Maybe these clues will help other newbies.

Regards,
Al
 
Old January 26th, 2007, 12:25 AM
Registered User
 
Join Date: Jan 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Al,
Just curious what version of mysql you are using in your setup. I've installed with 5.0, but I'm thinking you may have been working with 4.x.
I read somewhere that the 5.0 doesn't work well with the alias across tables, but I'm not sure.
Anyway, your work helped with my problems in the same area, so thanks for posting it.
-jeff






Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 4 Example Code Errors beckerm13 BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 September 28th, 2006 03:40 PM
Chapter 10 Sample Code jramkawsky BOOK: Professional Java Development with the Spring Framework 0 December 3rd, 2005 08:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.