0

I tried but it doesn't work. How can I make it display a message like "No Results were found" when nothing is returned?

My PHP script is:

<?php
$pnam=$_POST["prodnam"];
if($pnam=="")
{
  echo("Please enter a search…");
  return;
}

include("connection.php");

$SQL="SELECT * FROM results WHERE prodnam='".$pnam."'";
$run=mySQL_query($SQL,$con) or die ("SQL query error"); 
$rec=mysql_fetch_array($run);

echo ("<table align='center' style='
  font-family: Arial, Helvetica, sans-serif;
  color:#ffffff;
  font-size: 15px;
  text-align:center;'>");
echo ("<tr>");
echo ("<td>".$rec["dtlsnam"]."</td>");
echo ("</tr>");
echo ("</table>");
?>
adamdunson
  • 2,569
  • 1
  • 21
  • 27
  • 5
    You are **wide open** to SQL injection, and you **will be hacked** if you haven't been already. Learn to use prepared/parameterized queries with PDO or similar to avoid this problem entirely. – Brad Jan 15 '13 at 04:13
  • have u solved out your problem? then accept answerby clicking the tick icon below upvote downvote button – عثمان غني Jan 15 '13 at 04:40

5 Answers5

1

Apart from stating the obvious statement to use PDO or mysqli, here's the function you need: mysql_num_rows().

So, just do check as follows:

if( mysql_num_rows($run) == 0 ) exit( "No results" );
hjpotter92
  • 75,209
  • 33
  • 136
  • 171
0

Use following code to check if records fetched by your query.

$rec=mysql_fetch_array($run);
if(mysql_num_rows($run)>0)
{

// messaege here

}

And also reduce the risk of SQL injection by using following code

$pnam = stripslashes($pnam);
عثمان غني
  • 2,706
  • 4
  • 49
  • 78
0

use mysql_num_rows for checking how many rows query returns and according to that handle the situation.

 $run=mySQL_query($SQL,$con) or die ("SQL query error");
 if(mysql_num_rows($run)>0) { 
        $rec=mysql_fetch_array($run);



        echo ("<table align='center' style='
            font-family: Arial, Helvetica, sans-serif;color:#ffffff;
            font-size: 15px;
            text-align:center;'>");
        echo ("<tr>");
        echo ("<td>".$rec["dtlsnam"]."</td>");
        echo ("</tr>");
        echo ("</table>");
}
else
{
    echo 'no rows present';
}
Yogesh Suthar
  • 30,136
  • 18
  • 69
  • 98
0

You can use mysql_num_rows() to get the number of records returned.

<?php

$num_rows = mysql_num_rows($run);

if($num_rows==0)
{
  echo 'No records found';
}
else
{
 $rec=mysql_fetch_array($run);

  echo ("<table align='center' style='
                font-family: Arial, Helvetica, sans-serif;color:#ffffff;
                font-size: 15px;
                text-align:center;'>");
            echo ("<tr>");
            echo ("<td>".$rec["dtlsnam"]."</td>");
            echo ("</tr>");
            echo ("</table>");

}

?>
cartina
  • 1,403
  • 12
  • 20
0

I am just switching my queries from mysql_ to PDO as well. Rather than just telling you to change, I will try my best to get it right in PDO... if I mess up, I will learn something. Hopefully, you can take that and build from it and improve what you are doing as I continue to improve my queries. I only have one small app upgraded with a few big ones to go. Still in learning phase.

I will be honest, I never would have upgraded if I haven't gotten on stack overflow all the time now. Every person who uses the mysql_connect gets hit on it. Made me get outside of my little programmer box and notice. Thanks guys!

OK, I am about to mess this up, so don't laugh.

P.S. Can't believe no one mentioned the Table in the question. What happens if more than one product has the same name?

$stmt=$con->prepare("SELECT * FROM results WHERE prodnam=:pnam");  // named variables in prepared
$stmt->bindValue(':pnam', $pnam, PDO::PARAM_STR);  // bind it to the variable
$stmt->execute();
$hit = $stmt->rowCount();  // count them rows

if($hit) {
   while($results = $stmt->fetchAll(PDO::FETCH_ASSOC)) {
      echo $results['dtlsnam'];
      /* I ignored the fact you are using tables because maybe it is a good tabular layout 
* you are planning.  You can figure out the table part.  But best to loop and put the table
*start before the loop and the table end after the loop and just loop the rows.
*/
   }
}

//
// Then in your connection include you want something like this.
//
// put in the values for your variables.
<?php
try {
$con = new PDO("mysql:host=" . $DB_MYSQL_HOST. ";dbname=" . $DB_MYSQL_NAME, $DB_MYSQL_USER,   $DB_MYSQL_PASS);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
donlaur
  • 1,261
  • 1
  • 12
  • 21