Closed Thread
Results 1 to 9 of 9

Thread: php help, whats wrong? just a few lines of code...

  1. #1
    Junior Member mjz is an unknown quantity at this point
    Join Date
    Feb 2004
    Posts
    29

    php help, whats wrong? just a few lines of code...

    edit: okey, i got some help and its fixed now...

    old code:

    Code:
    if ($_GET["cat"])
    {
     $query = "select * from products where cat = $_GET['cat']";
    
     $res = mysql_query ($query) or
                 die (mysql_error());
    
     echo "<table>";
    
     while ($row = mysql_fetch_array($res))
     {
       echo "<tr><td>";
       echo $row["prodnr"];
       echo "</td></tr>";
     }
     mysql_free_result($res);
    
     echo "</table>";
    }
    working code:

    Code:
    if ($_GET["cat"])
    {
    
     $getcat = $_GET["cat"];
    
     $query = "select * from products where cat = '" . $_GET['cat'] . "'";
    
     $res = mysql_query ($query) or
                 die (mysql_error());
    
     echo "<table>";
    
     while ($row = mysql_fetch_array($res))
     {
       echo "<tr><td>";
       echo $row["prodname"];
       echo "</td></tr>";
     }
     mysql_free_result($res);
    
     echo "</table>";
    }
    Last edited by mjz; February 8th, 2004 at 12:53.

  2. #2
    W as in Whisky Wojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to behold Wojtek's Avatar
    Join Date
    Nov 2000
    Location
    Montreal, Canada
    Posts
    6,275
    Im no Php guru but here are some things you might wanna double check:



    if ($_GET["cat"])
    $_GET['cat']";

    You used double quotes on first, single quotes on second


    instead of $row["name"];, try $row[0], or $row[1], depending of the pos of the row
    -W

  3. #3
    Junior Member mjz is an unknown quantity at this point
    Join Date
    Feb 2004
    Posts
    29
    yeah, but doube or single quote shouldn't make any difference in this case, and it doesn't, i've tried... and neither does using number instead of names... but thanks for looking at the code...

  4. #4
    Senior Member bloodyveins is an unknown quantity at this point bloodyveins's Avatar
    Join Date
    Mar 2003
    Location
    squid.conf
    Posts
    230
    fundamentals of quotes:
    -single quote -> enclose statement which contains no variables;
    -double quote -> enclose statement which may contain variables;
    -no quote -> let php interprets the statement itself

    valid:
    $string = "I saw my $_GET[buddy]";
    $string = "I saw my ".$_GET['buddy'];
    $string = "I saw my ".$_GET["buddy"];

    invalid:
    $string = "I saw my $_GET['buddy']"; --> leads to empty output
    $string = "I saw my $_GET["buddy"]"; --> parsed error

  5. #5
    W as in Whisky Wojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to beholdWojtek is a splendid one to behold Wojtek's Avatar
    Join Date
    Nov 2000
    Location
    Montreal, Canada
    Posts
    6,275
    bloodyveins = Official FWS Php Guru
    -W

  6. #6
    FWS Addict spec is an unknown quantity at this point
    Join Date
    Jun 2001
    Posts
    712
    for clerification single quotes in an unparsed string, double quotes is a parsed string. no quotes is either a boolean or numeric datatype

  7. #7
    Doctor Hexagon Canuckkev is just really niceCanuckkev is just really niceCanuckkev is just really niceCanuckkev is just really nice Canuckkev's Avatar
    Join Date
    Dec 2000
    Location
    Calgary, Canada
    Posts
    3,582
    Something of purely no interest to anyone unless they care about miniscule seconds of parse time, using $_GET['var'] will parse faster than $_GET["var"]. PLUS, you press TWO LESS KEYS when typing the former.

  8. #8
    anti-liberal keith is an unknown quantity at this point keith's Avatar
    Join Date
    Oct 2000
    Location
    Buttsville
    Posts
    2,375
    Originally posted by Wojtek
    instead of $row["name"];, try $row[0], or $row[1], depending of the pos of the row
    what?? $row['name'] comes very highly preferred by yours truly over $row['0'], $row['1'], etc... much easier to organize and keep track of, especially if you are pulling info from a large number of rows.
    w3rd

  9. #9
    Senior Member bloodyveins is an unknown quantity at this point bloodyveins's Avatar
    Join Date
    Mar 2003
    Location
    squid.conf
    Posts
    230
    Something of purely no interest to anyone unless they care about miniscule seconds of parse time, using $_GET parse faster than $_GET["var"]. PLUS, you press TWO LESS KEYS when typing the former.
    yeah... agree...

Closed Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts