p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 (http://p2p.wrox.com/forumdisplay.php?f=476)
-   -   Chp 10: mysql command question (http://p2p.wrox.com/showthread.php?t=80186)

DMatt July 20th, 2010 10:09 AM

Chp 10: mysql command question
 
Hi, i've a question about mysql command in the chp 10 (and not only).

I don't understand the syntax of this:
PHP Code:

if ($character_id != 0) {
    
$query 'SELECT
        c.alias, c.real_name, c.alignment,
        l.address, z.city, z.state, z.zipcode_id
        FROM
            comic_character c, comic_lair l, comic_zipcode z
        WHERE
            z.zipcode_id = l.zipcode_id AND
            c.lair_id = l.lair_id AND
            c.character_id = ' 
$character_id;
    
$result mysql_query($query$db) or die (mysql_error($db)); 

I mean the "c." , "l." , "z." and the "comic_character c, comic_lair l, comic_zipcode z", why i have to insert c, l and z? how it works?

Thanks

Old Pedant July 22nd, 2010 06:36 PM

Those are just "aliases". A far *BETTER* way to write that would be:
Code:

  $query = 'SELECT
      c.alias, c.real_name, c.alignment,
      l.address, z.city, z.state, z.zipcode_id
      FROM
          comic_character AS c, comic_lair AS l, comic_zipcode AS z
      WHERE
          z.zipcode_id = l.zipcode_id AND
          c.lair_id = l.lair_id AND
          c.character_id = '
. $character_id
;

So that the AS clearly says "this is an alias I am using."

That is, those are "shortcut" names given to those tables, for convenience.


That code is 100% equivalent to
Code:

  $query = 'SELECT
      comic_character.alias, comic_character.real_name, comic_character.alignment,
      comic_lair.address, comic_zipcode.city, comic_zipcode.state, comic_zipcode.zipcode_id

      FROM comic_character, comic_lair, comic_zipcode
      WHERE
          comic_zipcode.zipcode_id = comic_lair.zipcode_id AND
          comic_character.lair_id = comic_lair.lair_id AND
          comic_character.character_id = '
. $character_id
;

You see? It makes the code easier to read, but other than that it has *ZERO* impact on the query.

DMatt July 26th, 2010 08:39 AM

Perfect! Thanks!

bopjo1 October 3rd, 2010 07:38 PM

Why the hell would they put that in a beginners book, suddenly, without ANY explanation whatsoever? It really pisses me off!!! The same thing a few pages before, where they casually inject the sprintf() function into the code with literally ZERO explanation.

DMatt October 4th, 2010 05:58 PM

I asked to me the same things... Now i'm at chp 16 and more and more script are totaly WRONGS. The funny thing is that in the book there are images that show the correct work of the scripts just writings.. the problem is that doesn't works.. so the real working script where are??? In the book sure not! Make fun of us!

bopjo1 October 6th, 2010 03:52 PM

Did you ever get this application to work? The only thing that doesn't work for me is if I click on one of the aliases in "list_characters.php" to edit. It goes to the edit page but none of the character's data is entered into the form fields like it is supposed to. I think it has something to do with the MySQL query in your first post.

Were you able to edit characters?

DMatt October 6th, 2010 04:33 PM

Hi, i don't remind exactly what i done for fix the problem but is fixed, i can add-remove-edit any characters i want.. however, i fixed a lot of problems found in the book... not only this one... so boring..

bopjo1 October 6th, 2010 04:49 PM

Did you remember if you used the exact MySQL code in your first post? (this one:)

Code:

$query = 'SELECT
      c.alias, c.real_name, c.alignment,
      l.address, z.city, z.state, z.zipcode_id
      FROM
          comic_character AS c, comic_lair AS l, comic_zipcode AS z
      WHERE
          z.zipcode_id = l.zipcode_id AND
          c.lair_id = l.lair_id AND
          c.character_id = '. $character_id;


DMatt October 6th, 2010 05:02 PM

Yes, in edit_character.php is the same, i have just checked

Code:

if ($character_id != 0) {
    $query = 'SELECT
        c.alias, c.real_name, c.alignment,
        l.address, z.city, z.state, z.zipcode_id
        FROM
            comic_character c, comic_lair l, comic_zipcode z
        WHERE
            z.zipcode_id = l.zipcode_id AND
            c.lair_id = l.lair_id AND
            c.character_id = ' . $character_id;
    $result = mysql_query($query, $db) or die (mysql_error($db));

the problem is not in these rows... if you describe to me exactly what's the output generated by the script i help you to found it, or if you want i can post the whole script..

-EDIT-

hey take care about the "AS"... don't include that in the rows!!

bopjo1 October 6th, 2010 05:48 PM

I isolated just the MySQL query exactly as written (without the AS) and put it in another file with an echo statement to see if it works like this:

Code:

<?php
require 'includes/db.inc.php';
$db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD) or die ('Unable to connect');
mysql_select_db(MYSQL_DB, $db) or die(mysql_error($db));
        $query = 'SELECT
      c.alias, c.real_name, c.alignment,
      l.address, z.city, z.state, z.zipcode_id
      FROM
          comic_character c, comic_lair l, comic_zipcode AS z
      WHERE
          z.zipcode_id = l.zipcode_id AND
          c.lair_id = l.lair_id AND
          c.character_id = '. $character_id;

$result = mysql_query($query, $db) or die(mysql_error($db));

if (mysql_num_rows($result) > 0) {
        $action = 'Edit';
        $character = mysql_fetch_assoc($result);
        echo $character['alias'];
}
?>

And I keep getting this error:

"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 9"

So there's something weird going on with that MySQL code...

If you have your version of edit_character.php handy I would appreciate it very much if you would post it. Thanks!


All times are GMT -4. The time now is 09:21 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.