...

View Full Version : Resolved Problem with _GET and database



SP360
08-30-2009, 09:52 PM
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
$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";
}
?>

_Aerospace_Eng_
08-30-2009, 09:58 PM
This isn't what you want. Change this

if($code=="$code")
{
echo "$code";
}
else
{
echo "defaultcode";
}
to this

if(mysql_num_rows($data) == 1)
{
echo $code;
}
else
{
echo "defaultcode";
}

SP360
08-30-2009, 10:23 PM
This isn't what you want. Change this

if($code=="$code")
{
echo "$code";
}
else
{
echo "defaultcode";
}
to this

if(mysql_num_rows($data) == 1)
{
echo $code;
}
else
{
echo "defaultcode";
}


Thank you very much _Aerospace_Eng_,

Really did the trick :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum