-1

i just trying to create php form

   <?php 

    $category = $_POST['category'];  
    $length = $_POST['length'];  
    $con = mysql_connect("localhost","globalex","[?H~hS=Oc=ES");
     $db = mysql_select_db("globalex",$con);
     $query = "SELECT list FROM answers WHERE category = '$category' AND length =    '$length'";
    $result = mysql_query($query);
    while($row = mysql_fetch_array($result)) {
    echo 'Category '. $category .' '. $length .' letters: ';
    echo '<b>'. $row['0'].'</b><br>';
    } 
    ?>

how to add message not found when the query has no result? thank you.

Budi Andug
  • 33
  • 2

2 Answers2

1

You shouldn't use the mysql_ functions anymore the are deprecated and don't allow you to use parameterized queries which will prevent you from being SQL injected.

Here's a way using the mysql_ functions. You can easily transistion this to mysqli or pdo because they both have the num_rows function just check the manual for their implementation.

$category = mysql_real_escape_string($_POST['category']);  
$length = mysql_real_escape_string($_POST['length']);  
$con = mysql_connect("localhost","globalex","[?H~hS=Oc=ES");
 $db = mysql_select_db("globalex",$con);
 $query = "SELECT list FROM answers WHERE category = '$category' AND length =    '$length'";
$result = mysql_query($query) or die(mysql_error());
if(mysql_num_rows($result) == 0) {
      echo "No Results";
} else { 
     while($row = mysql_fetch_array($result)) {
           echo 'Category '. $category .' '. $length .' letters: ';
           echo '<b>'. $row['0'].'</b><br>';
     }
}

You can read more about SQL injection prevention here, How can I prevent SQL injection in PHP? and more about why the mysql_ functions shouldn't be used here Why shouldn't I use mysql_* functions in PHP?.

Community
  • 1
  • 1
chris85
  • 23,591
  • 7
  • 30
  • 47
0
<?php 

$category = $_POST['category'];  
$length = $_POST['length'];  
$con = mysql_connect("localhost","globalex","[?H~hS=Oc=ES");
 $db = mysql_select_db("globalex",$con);
 $query = "SELECT list FROM answers WHERE category = '$category' AND length =    '$length'";
$result = mysql_query($query);
if(mysqli_num_rows($result)>0){
while($row = mysql_fetch_array($result)) {
echo 'Category '. $category .' '. $length .' letters: ';
echo '<b>'. $row['0'].'</b><br>';
} 

}else{
echo 'no result';}
?>
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
Virendra Nagda
  • 645
  • 5
  • 9