Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Problem with _GET and database

    Hi there,

    What I'm trying to achieve with the following code is for the script to search in the database for 'ref' value, and if it doesnt exist or not supplied then display 'defaultcode'.

    However, this has confused me a lot. If I do supply an existing 'ref' value that is in the database I get the actual $code value, but if I supply a value that's not in the database or not supply anything then the 'defaultcode' text is not echoed.

    Please let me know what the problem may be.

    Thanks

    PHP Code:
    <?php 
    $ref 
    htmlspecialchars(@$_GET["ref"]);
    $data mysql_query("SELECT * FROM table WHERE ref='$ref'") or die(mysql_error());
    $info mysql_fetch_array$data );
    $code $info['code'];

        if(
    $code=="$code")
        {
             echo 
    "$code";
        }
        else
        {
            echo 
    "defaultcode";
        }
    ?>
    Last edited by SP360; 08-30-2009 at 10:23 PM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    This isn't what you want. Change this
    PHP Code:
        if($code=="$code")
        {
             echo 
    "$code";
        }
        else
        {
            echo 
    "defaultcode";
        } 
    to this
    PHP Code:
        if(mysql_num_rows($data) == 1)
        {
             echo 
    $code;
        }
        else
        {
            echo 
    "defaultcode";
        } 
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    SP360 (08-30-2009)

  • #3
    New Coder
    Join Date
    Jan 2007
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    This isn't what you want. Change this
    PHP Code:
        if($code=="$code")
        {
             echo 
    "$code";
        }
        else
        {
            echo 
    "defaultcode";
        } 
    to this
    PHP Code:
        if(mysql_num_rows($data) == 1)
        {
             echo 
    $code;
        }
        else
        {
            echo 
    "defaultcode";
        } 

    Thank you very much _Aerospace_Eng_,

    Really did the trick


  •  

    Posting Permissions

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