how to show "no record found" when searching record..!

  • asad_black
  • Graduate
  • Graduate
  • asad_black
  • Posts: 131
  • Loc: pakistan

Post 3+ Months Ago

I am new in PHP and this is my search code noW i want to display "NO RECORDS FOUND" when no record match with search string..!

this is my code:
Code: [ Select ]
<?php
$var = @$_GET['q'] ;
$trimmed = trim($var);
// database connection info
$conn = mysql_connect('localhost','asad','asad') or trigger_error("SQL", E_USER_ERROR);
$db = mysql_select_db('asad',$conn) or trigger_error("SQL", E_USER_ERROR);

// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM links where kw like \"%$trimmed%\"";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];

// number of rows to show per page
$rowsperpage = 3;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);

// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if

// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if

// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;

// get the info from the db
$sql = "SELECT * FROM links where kw like \"%$trimmed%\"
LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);


    echo "<table border='0' cellpadding='3' cellspacing='3' width='50%'>";

// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result)) {
// echo data

    {
echo "<tr>";
// echo "<td valign=top>". $list['text'];
echo "<td rowspan=2 valign=top><a href=\"idpage.php?id=" . $list["id"] . "\">
<img src=\"" . $list["image"] . "\" height='80' width='80' border=0 \"" . $list["text"] . "\"></a></td>";
echo "<td valign=top class=headingofvideotext height='29'><a href=\"idpage.php?id=" . $list["id"] . "\">". $list['text'];
echo "</td>";
echo "</tr>";

echo "<tr class=style28>";

echo "<td valign=top>" . $list['des'] . "</td>";
echo "</tr>";
}

}

echo "</td>";
    echo "</tr>";
echo "</table>";
/****** build the pagination links ******/
// range of num links to show
$range = 3;

// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&q=$var'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&q=$var'>Previous</a> ";
} // end if

// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
     // if we're on current page...
     if ($x == $currentpage) {
        // 'highlight' it but don't make a link
        echo " [<b>$x</b>] ";
     // if not current page...
     } else {
        // make it a link
    echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&q=$var'>$x</a> ";
     } // end else
} // end if
} // end for
    
// if not on last page, show forward and last page links 
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage + 1;
    // echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&q=$var'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&q=$var'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>
  1. <?php
  2. $var = @$_GET['q'] ;
  3. $trimmed = trim($var);
  4. // database connection info
  5. $conn = mysql_connect('localhost','asad','asad') or trigger_error("SQL", E_USER_ERROR);
  6. $db = mysql_select_db('asad',$conn) or trigger_error("SQL", E_USER_ERROR);
  7. // find out how many rows are in the table
  8. $sql = "SELECT COUNT(*) FROM links where kw like \"%$trimmed%\"";
  9. $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  10. $r = mysql_fetch_row($result);
  11. $numrows = $r[0];
  12. // number of rows to show per page
  13. $rowsperpage = 3;
  14. // find out total pages
  15. $totalpages = ceil($numrows / $rowsperpage);
  16. // get the current page or set a default
  17. if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
  18. // cast var as int
  19. $currentpage = (int) $_GET['currentpage'];
  20. } else {
  21. // default page num
  22. $currentpage = 1;
  23. } // end if
  24. // if current page is greater than total pages...
  25. if ($currentpage > $totalpages) {
  26. // set current page to last page
  27. $currentpage = $totalpages;
  28. } // end if
  29. // if current page is less than first page...
  30. if ($currentpage < 1) {
  31. // set current page to first page
  32. $currentpage = 1;
  33. } // end if
  34. // the offset of the list, based on current page
  35. $offset = ($currentpage - 1) * $rowsperpage;
  36. // get the info from the db
  37. $sql = "SELECT * FROM links where kw like \"%$trimmed%\"
  38. LIMIT $offset, $rowsperpage";
  39. $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
  40.     echo "<table border='0' cellpadding='3' cellspacing='3' width='50%'>";
  41. // while there are rows to be fetched...
  42. while ($list = mysql_fetch_assoc($result)) {
  43. // echo data
  44.     {
  45. echo "<tr>";
  46. // echo "<td valign=top>". $list['text'];
  47. echo "<td rowspan=2 valign=top><a href=\"idpage.php?id=" . $list["id"] . "\">
  48. <img src=\"" . $list["image"] . "\" height='80' width='80' border=0 \"" . $list["text"] . "\"></a></td>";
  49. echo "<td valign=top class=headingofvideotext height='29'><a href=\"idpage.php?id=" . $list["id"] . "\">". $list['text'];
  50. echo "</td>";
  51. echo "</tr>";
  52. echo "<tr class=style28>";
  53. echo "<td valign=top>" . $list['des'] . "</td>";
  54. echo "</tr>";
  55. }
  56. }
  57. echo "</td>";
  58.     echo "</tr>";
  59. echo "</table>";
  60. /****** build the pagination links ******/
  61. // range of num links to show
  62. $range = 3;
  63. // if not on page 1, don't show back links
  64. if ($currentpage > 1) {
  65. // show << link to go back to page 1
  66. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&q=$var'>First</a> ";
  67. // get previous page num
  68. $prevpage = $currentpage - 1;
  69. // show < link to go back to 1 page
  70. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&q=$var'>Previous</a> ";
  71. } // end if
  72. // loop to show links to range of pages around current page
  73. for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
  74. // if it's a valid page number...
  75. if (($x > 0) && ($x <= $totalpages)) {
  76.      // if we're on current page...
  77.      if ($x == $currentpage) {
  78.         // 'highlight' it but don't make a link
  79.         echo " [<b>$x</b>] ";
  80.      // if not current page...
  81.      } else {
  82.         // make it a link
  83.     echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x&q=$var'>$x</a> ";
  84.      } // end else
  85. } // end if
  86. } // end for
  87.     
  88. // if not on last page, show forward and last page links 
  89. if ($currentpage != $totalpages) {
  90. // get next page
  91. $nextpage = $currentpage + 1;
  92.     // echo forward link for next page
  93. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage&q=$var'>Next</a> ";
  94. // echo forward link for lastpage
  95. echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages&q=$var'>Last</a> ";
  96. } // end if
  97. /****** end build pagination links ******/
  98. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

PHP Code: [ Select ]
<?php
$search = 'Text to search';
 
$sql = "SELECT * FROM table_name WHERE field_name = $search";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
if($num < 1)
{
   echo 'NO RECORD FOUND';
}
 
// do other stuff here
?>
  1. <?php
  2. $search = 'Text to search';
  3.  
  4. $sql = "SELECT * FROM table_name WHERE field_name = $search";
  5. $result = mysql_query($sql);
  6. $num = mysql_num_rows($result);
  7. if($num < 1)
  8. {
  9.    echo 'NO RECORD FOUND';
  10. }
  11.  
  12. // do other stuff here
  13. ?>

That is the simple way to do that.
  • asad_black
  • Graduate
  • Graduate
  • asad_black
  • Posts: 131
  • Loc: pakistan

Post 3+ Months Ago

if($numrows == 0){
echo "NO RECORDS FOUND";
}

em using above code..!

but its shows also
paging at the bottom of the page...!
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

instead of echo you can use die("NO RECORD FOUND") if that's what you mean... or put exit; after the echo...

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 143 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.