views:

36

answers:

1

I have the following code:

<?php 

$code = $_POST['code'];   // textbox
$con = mysql_connect("***","****","****");
if (!$con)
{  
    die('Server overload, please try again' . mysql_error());
}

mysql_select_db("example", $con);

$sql = mysql_query("SELECT code FROM exampletable");

while ($version = mysql_fetch_array($sql))
{

     // do something

}

mysql_close($con);      

?>

What I want this code to do is check the value of an textbox then search the column code in my MySQL database to look for any matches. If there are any matches I would like it to check the column known as 'version' and if 'version' is equal to 1 then execute another piece of code.
I'm pretty new to MySQL and the MySQL/Database side of PHP so I know this will probably be an easy question for most of you to answer, so could anyone offer guidance?

Thanks,
Lawrence

+2  A: 

you must compare values using SQL, not PHP iterating over whole table. That's what SQL were invented for.

$code = mysql_real_escape_string($code); 
$sql = "SELECT code FROM exampletable WHERE code='$code' and version = 1"; 
$res = mysql_query($sql);
if (mysql_num_rows($res)) {
  echo "exact code found!";
} else {
  echo "not found";
}
Col. Shrapnel
version support added to the code
Col. Shrapnel