Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-30-2009, 09:52 PM   PM User | #1
SP360
New Coder

 
Join Date: Jan 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
SP360 is an unknown quantity at this point
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..
SP360 is offline   Reply With Quote
Old 08-30-2009, 09:58 PM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
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!||||
_Aerospace_Eng_ is offline   Reply With Quote
Users who have thanked _Aerospace_Eng_ for this post:
SP360 (08-30-2009)
Old 08-30-2009, 10:23 PM   PM User | #3
SP360
New Coder

 
Join Date: Jan 2007
Posts: 13
Thanks: 1
Thanked 0 Times in 0 Posts
SP360 is an unknown quantity at this point
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
SP360 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:48 AM.


Advertisement
Log in to turn off these ads.