Search script problem in php

  • sohang
  • Newbie
  • Newbie
  • sohang
  • Posts: 6

Post 3+ Months Ago

I have a Search script problem

I just follow up this code but it Couldn't execute query

Code: [ Select ]
 
 
<html>
<head>
<title>search script</title>
<meta name="author" content="Steve R">
</head>
<body>
 
<form name="form" action="searchphp.php" method="get">
  <input type="text" name="q" />
  <input type="submit" name="Submit" value="Search" />
</form>
 
<?php
 
  // Get the search variable from URL
 
  $var = @$_GET['q'] ;
  $trimmed = trim($var) ;//trim whitespace from the stored variable
 
// rows to return
$limit=10 ;
 
// check for an empty string and display a message.
if ($trimmed == "")
  {
  echo "<p>Please enter a search...</p>";
  exit;
  }
 
// check for a search parameter
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
 
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","root",""); //(host, username, password)
 
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("Faqn") or die("Unable to select database"); //select which database we're using
 
// Build SQL Query
$query = "SELECT *FROM que WHERE que LIKE \'%$trimmed%\'
  ORDERD BY id"; // EDIT HERE and specify your table and field names for the SQL query
 
 $numresults = mysql_query($query);
 $numrows = mysql_num_rows($numresults);
 
// If we have no results, offer a google search as an alternative
 
if ($numrows == 0)
  {
  echo "<h4>Results</h4>";
  echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
 
// google
 echo "<p><a href=\"http://www.google.com/search?q="
  . $trimmed . "\" target=\"_blank\" title=\"Look up
  " . $trimmed . " on Google\">Click here</a> to try the
  search on google</p>";
  }
 
// next determine if s has been passed to script, if not use 0
  if (empty($s)) {
  $s=0;
  }
 
// get results
  $query = " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
// display what the person searched for
echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
 
// begin to show results set
echo "Results";
$count = 1 + $s ;
 
// now you can display the results returned
  while ($row= mysql_fetch_array($result)) {
  $title = $row["1st_field"];
 
  echo "$count.)&nbsp;$title" ;
  $count++ ;
  }
 
$currPage = (($s/$limit) + 1);
 
//break before paging
  echo "<br />";
 
  // next we need to do the links to other results
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
  Prev 10</a>&nbsp&nbsp;";
  }
 
// calculate number of pages needing links
  $pages=intval($numrows/$limit);
 
// $pages now contains int of pages needed unless there is a remainder from division
 
  if ($numrows%$limit) {
  // has remainder so add one page
  $pages++;
  }
 
// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
 
  // not last page so give NEXT link
  $news=$s+$limit;
 
  echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p>Showing results $b to $a of $numrows</p>";
 
?>
 
<!-- © http://www.designplace.org/ -->
 
</body>
</html>
 
 
  1.  
  2.  
  3. <html>
  4. <head>
  5. <title>search script</title>
  6. <meta name="author" content="Steve R">
  7. </head>
  8. <body>
  9.  
  10. <form name="form" action="searchphp.php" method="get">
  11.   <input type="text" name="q" />
  12.   <input type="submit" name="Submit" value="Search" />
  13. </form>
  14.  
  15. <?php
  16.  
  17.   // Get the search variable from URL
  18.  
  19.   $var = @$_GET['q'] ;
  20.   $trimmed = trim($var) ;//trim whitespace from the stored variable
  21.  
  22. // rows to return
  23. $limit=10 ;
  24.  
  25. // check for an empty string and display a message.
  26. if ($trimmed == "")
  27.   {
  28.   echo "<p>Please enter a search...</p>";
  29.   exit;
  30.   }
  31.  
  32. // check for a search parameter
  33. if (!isset($var))
  34.   {
  35.   echo "<p>We dont seem to have a search parameter!</p>";
  36.   exit;
  37.   }
  38.  
  39. //connect to your database ** EDIT REQUIRED HERE **
  40. mysql_connect("localhost","root",""); //(host, username, password)
  41.  
  42. //specify database ** EDIT REQUIRED HERE **
  43. mysql_select_db("Faqn") or die("Unable to select database"); //select which database we're using
  44.  
  45. // Build SQL Query
  46. $query = "SELECT *FROM que WHERE que LIKE \'%$trimmed%\'
  47.   ORDERD BY id"; // EDIT HERE and specify your table and field names for the SQL query
  48.  
  49.  $numresults = mysql_query($query);
  50.  $numrows = mysql_num_rows($numresults);
  51.  
  52. // If we have no results, offer a google search as an alternative
  53.  
  54. if ($numrows == 0)
  55.   {
  56.   echo "<h4>Results</h4>";
  57.   echo "<p>Sorry, your search: &quot;" . $trimmed . "&quot; returned zero results</p>";
  58.  
  59. // google
  60.  echo "<p><a href=\"http://www.google.com/search?q="
  61.   . $trimmed . "\" target=\"_blank\" title=\"Look up
  62.   " . $trimmed . " on Google\">Click here</a> to try the
  63.   search on google</p>";
  64.   }
  65.  
  66. // next determine if s has been passed to script, if not use 0
  67.   if (empty($s)) {
  68.   $s=0;
  69.   }
  70.  
  71. // get results
  72.   $query = " limit $s,$limit";
  73.   $result = mysql_query($query) or die("Couldn't execute query");
  74.  
  75. // display what the person searched for
  76. echo "<p>You searched for: &quot;" . $var . "&quot;</p>";
  77.  
  78. // begin to show results set
  79. echo "Results";
  80. $count = 1 + $s ;
  81.  
  82. // now you can display the results returned
  83.   while ($row= mysql_fetch_array($result)) {
  84.   $title = $row["1st_field"];
  85.  
  86.   echo "$count.)&nbsp;$title" ;
  87.   $count++ ;
  88.   }
  89.  
  90. $currPage = (($s/$limit) + 1);
  91.  
  92. //break before paging
  93.   echo "<br />";
  94.  
  95.   // next we need to do the links to other results
  96.   if ($s>=1) { // bypass PREV link if s is 0
  97.   $prevs=($s-$limit);
  98.   print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt;
  99.   Prev 10</a>&nbsp&nbsp;";
  100.   }
  101.  
  102. // calculate number of pages needing links
  103.   $pages=intval($numrows/$limit);
  104.  
  105. // $pages now contains int of pages needed unless there is a remainder from division
  106.  
  107.   if ($numrows%$limit) {
  108.   // has remainder so add one page
  109.   $pages++;
  110.   }
  111.  
  112. // check to see if last page
  113.   if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {
  114.  
  115.   // not last page so give NEXT link
  116.   $news=$s+$limit;
  117.  
  118.   echo "&nbsp;<a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 &gt;&gt;</a>";
  119.   }
  120.  
  121. $a = $s + ($limit) ;
  122.   if ($a > $numrows) { $a = $numrows ; }
  123.   $b = $s + 1 ;
  124.   echo "<p>Showing results $b to $a of $numrows</p>";
  125.  
  126. ?>
  127.  
  128. <!-- © http://www.designplace.org/ -->
  129.  
  130. </body>
  131. </html>
  132.  
  133.  


It shows msg
Quote:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\FAQn\searchphp.php on line 48
Results

Sorry, your search: "a" returned zero results

Click here to try the search on google
Couldn't execute query


Where should have problem

Please help ......
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6252
  • Loc: Seattle, WA

Post 3+ Months Ago

Change this line:
Code: [ Select ]
$query = " limit $s,$limit";

to this:
Code: [ Select ]
$query .= " limit $s,$limit";
  • sohang
  • Newbie
  • Newbie
  • sohang
  • Posts: 6

Post 3+ Months Ago

thanks your comment but although there is a problem

its not working

problem is same

Code: [ Select ]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\FAQn\searchphp.php on line 48
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

Change this:
Code: [ Select ]
$query = "SELECT *FROM que WHERE que LIKE \'%$trimmed%\'
  ORDERD BY id"; // EDIT HERE and specify your table and field names for the SQL query
  1. $query = "SELECT *FROM que WHERE que LIKE \'%$trimmed%\'
  2.   ORDERD BY id"; // EDIT HERE and specify your table and field names for the SQL query

to this:
Code: [ Select ]
$query = "SELECT * FROM que WHERE que LIKE \'%$trimmed%\' ORDER BY id ASC"; // EDIT HERE and specify your table and field names for the SQL query

note there are 2 changes there (the * was right next to the FROM and ORDER was spelled ORDERD).

And just to make sure, you DO have a table named que with fields named que and id in it, right?

ALSO: That script is ripe for sql injection. You really need to use mysql_real_escape_string() on any input that will be used in a sql query.

Edit: Better? :P
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

You can try the following for some troubleshooting purposes.

Code: [ Select ]
$numresults = mysql_query($query) or die(mysql_error());


@ScienceOfSpock: It is obvious that you are using the dark theme... that yellow goes horrible with the light theme ;)

Edit: Yeah :)
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Heyyyy Spock, I was just thinkin bout you the other day wondering where ya were when someone brought up Captcha.
  • ScienceOfSpock
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1893
  • Loc: Las Vegas

Post 3+ Months Ago

UPSGuy wrote:
Heyyyy Spock, I was just thinkin bout you the other day wondering where ya were when someone brought up Captcha.


I've been very busy lately, and didn't have time to visit Ozzu but once every week or two. Was working for Filefront.com but the job disappeared at the end of March when Ziff Davis decided to kill the site, so I've got some free time now :)
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Didn't they sell it back to the original founders of the website? I read about that this morning. They hosted a cool gaming mod that I'm interested in.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8397
  • Loc: USA

Post 3+ Months Ago

PolishHurricane wrote:
Didn't they sell it back to the original founders of the website? I read about that this morning. They hosted a cool gaming mod that I'm interested in.

They did... the 'founders' as they called it, bought it... that is why the site is still working. That is what I read about it about an hour or so ago.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Cool, small world.

Post Information

  • Total Posts in this topic: 10 posts
  • Users browsing this forum: No registered users and 166 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
 
cron
 

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