SitePoint Sponsor

User Tag List

Results 1 to 25 of 25
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question What is wrong with my code?

    When I navigate to this url:
    site.com/verify.php?key=0000-0000

    a 0 or 1 should display, instead I get a syntax error.

    It is connecting to the database correctly... Ive tried and tried to make it work, something is missing.


    PHP Code:
    <?php
    $key 
    $_GET['key'];
    $trimmed trim($key); //trim whitespace from the stored variable


    mysql_connect("dburl""username""password") or die(mysql_error());

    mysql_select_db("dbname") or die(mysql_error());

    $query "SELECT * FROM auth WHERE key LIKE '$trimmed'";  
        
         
    $result mysql_query($query) or die(mysql_error());



    while(
    $row mysql_fetch_array($result)){
    echo 
    $row['key']. "<br />" $row['active'];


    }

    ?>

  2. #2
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well what is the syntax error?

    By the way, I have 2 suggestions... add mysql_real_escape_string to $key and use WHERE key = '$key otherwise it will give you a loose match, unless thats what you want.
    PHP Code:

    <?php

    $key 
    $_GET['key'];

    $trimmed mysql_real_escape_string(trim($key)); //trim whitespace from the stored variable

    mysql_connect("dburl""username""password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());

    $query mysql_query("SELECT * FROM auth WHERE key =  '$trimmed'") or die(mysql_error());  

    while(
    $row mysql_fetch_array($query)) 
    {
        echo 
    $row['key']. "<br />" $row['active'];
    }



    ?>
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The syntax error is:
    PHP Code:
    You have an error in your SQL syntaxCheck the manual that corresponds to your MySQL server version for the right syntax to use near 'key LIKE '0000-0000'' at line 1 

    Quote Originally Posted by wheeler View Post
    well what is the syntax error?

    By the way, I have 2 suggestions... add mysql_real_escape_string to $key and use WHERE key = '$key otherwise it will give you a loose match, unless thats what you want.
    PHP Code:

    <?php

    $key 
    $_GET['key'];

    $trimmed mysql_real_escape_string(trim($key)); //trim whitespace from the stored variable

    mysql_connect("dburl""username""password") or die(mysql_error());
    mysql_select_db("dbname") or die(mysql_error());

    $query mysql_query("SELECT * FROM auth WHERE key =  '$trimmed'") or die(mysql_error());  

    while(
    $row mysql_fetch_array($query)) 
    {
        echo 
    $row['key']. "<br />" $row['active'];
    }



    ?>

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    If the key is empty the database is doing to throw an error. Use a condition below the select database.

    Code PHP:
    if ($trimmed) {
      $query = mysql_query("SELECT * FROM auth WHERE key =  '$trimmed'") or die(mysql_error());  
      while($row = mysql_fetch_array($query)) 
     
      {
        echo $row['key']. "<br />" . $row['active'];
      }
    }

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the syntax error even with your code.


    PHP Code:
    You have an error in your SQL syntaxCheck the manual that corresponds to your MySQL server version for the right syntax to use near 'key = '0000-0000'' at line 1 
    It has something to search for, that isn't the problem.


    Quote Originally Posted by pmw57 View Post
    If the key is empty the database is doing to throw an error. Use a condition below the select database.

    Code PHP:
    if ($trimmed) {
      $query = mysql_query("SELECT * FROM auth WHERE key =  '$trimmed'") or die(mysql_error());  
      while($row = mysql_fetch_array($query)) 
     
      {
        echo $row['key']. "<br />" . $row['active'];
      }
    }

  6. #6
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm doing something wrong.

    Should i not be using "LIKE" ?

    What I want to do is make this page:
    site.com/verify.php/?key=0000-0000

    Display a 0 if NOT in the database

    and a 1 if it IS in the database.

    The database insert is not the problem.

    I want the script above to simply return the key and active or not (0 or 1)

    echo $row['key']. "<br />" . $row['active'];

  7. #7
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    LIKE provides a broad match, if your key should be an exact match then just use equals as per my example.

    The syntax looks 100&#37; correct to me. Are you sure you are testing the correct file - have you uploaded the changes?
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  8. #8
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just double checked to make sure the newest file is on the server.

    Please see:
    http://erbio.net/verify.php?key=0000-0000

    Notice the syntax error changed and does not include "LIKE" but the equals now:
    PHP Code:
    You have an error in your SQL syntaxCheck the manual that corresponds to your MySQL server version for the right syntax to use near 'key = '0000-0000'' at line 1 

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The only thing that can be discerned from the error statement that's different from expected usage is that you have whitespace around the equals sign.

    Please use var_dump($query) to display the query string, because there is obviously something about the query that is wrong.

  10. #10
    SitePoint Wizard wheeler's Avatar
    Join Date
    Mar 2006
    Location
    Gold Coast, Australia
    Posts
    1,369
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    also can you paste in your current script
    Studiotime - Time Management for Web Developers
    to-do's, messages, invoicing, reporting - 30 day free trial!
    Thomas Multimedia Web Development

  11. #11
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  12. #12
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Use the following:

    Code PHP:
    if ($trimmed) {
      $query = "SELECT * FROM auth WHERE key =  '$trimmed'"
      var_dump($query);
      $result = mysql_query($query) or die(mysql_error()); 
      while($row = mysql_fetch_array($result)) {
        echo $row['key']. "<br />" . $row['active'];
      }
    }

  13. #13
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, updated with the code you provided and I get this:

    PHP Code:
    Parse errorsyntax errorunexpected T_STRING in D:\hshome\servicep\erbio.net\verify.php on line 23 
    Line 23 is:
    PHP Code:
      var_dump($query); 
    Quote Originally Posted by pmw57 View Post
    Use the following:

    Code PHP:
    if ($trimmed) {
      $query = "SELECT * FROM auth WHERE key =  '$trimmed'"
      var_dump($query);
      $result = mysql_query($query) or die(mysql_error()); 
      while($row = mysql_fetch_array($result)) {
        echo $row['key']. "<br />" . $row['active'];
      }
    }

  14. #14
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    My apologies, make that echo var_dump($query);

  15. #15
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    My apologies, make that echo var_dump($query);
    Ok updated
    PHP Code:
    string(43"SELECT * FROM auth WHERE key = '0000-0000'" You have an error in your SQL syntaxCheck the manual that corresponds to your MySQL server version for the right syntax to use near 'key = '0000-0000'' at line 1 

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I think this was mentioned before, but get rid of the whitespace around the equal sign

  17. #17
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I just removed the spaces and updated the file...

    Quote Originally Posted by pmw57 View Post
    I think this was mentioned before, but get rid of the whitespace around the equal sign

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I'm pretty sure that key is a reserved word. Put quotes around it.

  19. #19
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, that was the problem.

    Now I dont understand why the "active" isn't appearing.

    PHP Code:
    $result mysql_query($query) or die(mysql_error());
      while(
    $row mysql_fetch_array($result)) {
        echo 
    $row['active'];
      } 

  20. #20
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's all just blank

  21. #21
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nevermind, I just changed key to pkey

  22. #22
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Find out how many rows were returned.

    Code PHP:
    $result = mysql_query($query) or die(mysql_error());
    echo 'Number of results: ' . mysql_num_rows($result);
    while($row = mysql_fetch_array($result)) {
      echo $row['active'];
    }

  23. #23
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now this isn't working for me:
    PHP Code:
    if ($status "0"); {
        echo 
    "not-authorized";
        }
        
        if (
    $status "1"); {
        echo 
    "authorized for use";
        }
        
        if (
    $status "2"); {
        echo 
    "refunded";
        } 
    http://erbio.net/verify.php?key=1234-1234

    the active for this key above is "1"

    and should display "authorized for use"

  24. #24
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    pmw57, are you available via AOL instant messenger?

    Interested in being paid to whip up a bit of code for me?

    If so, please let me know what you'd charge.

  25. #25
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Will do


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
  •