Search Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Search Script

Post by wolfnipplechips »

Hi there,

Below is a search script. It works fine when selecting results from one column, but when i try to select from multiple columns I get the following error...

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

The error is on the line containing the sql query.

Code: Select all

 
<?php
 
  $var = @$_GET['q'] ;
  $trimmed = trim($var); 
 
$limit=10; 
 
if ($trimmed == "")
  {
  echo "<span class='bodytext'> The search field was empty - please try again using the search form above </span><br>
<br>
<span class='bodytext'>If you are experiencing problems with this site please contact us <a href='contact_details.php' class='bodybold'>here</a></span>";
  exit;
  }
 
if (!isset($var))
  {
  echo "<p>We dont seem to have a search parameter!</p>";
  exit;
  }
 
include("course_scripts/connect.php"); 
 
$query = "select * from course where courseTitle like \"%$trimmed%\ OR courseDescription like \"%$trimmed%\"  
  order by courseTitle";
 
 $numresults=mysql_query($query);
 $numrows=mysql_num_rows($numresults);
 
 
if ($numrows == 0)
  {
  echo "<p class='bodytext'>Sorry, your search:<span class='numbers'> "" . $trimmed 
  . ""</span> returned zero results</p>";
 
  }
 
  if (empty($s)) {
  $s=0;
  }
 
  $query .= " limit $s,$limit";
  $result = mysql_query($query) or die("Couldn't execute query");
 
echo "<table width='535' align='center' border='0' cellpadding='2' cellspacing='2' class='boxes'>
  <tr> 
    <td><p class='titles'>You searched for: <span class='numbers'>"" . 
        $var . ""<br>
        </span></td>
  </tr>
</table><br>";
$count = 1 + $s ;
 
  while ($row= mysql_fetch_array($result)) {
  $courseTitle = $row["courseTitle"];
      $courseID=$row["courseID"];
 
  echo "<span class='bodybold'>$courseTitle - </span>";
 
echo '<a href="frontend_scripts/show_course.php?courseID='.$row["courseID"].'" class="links">View This Course</a><br><br>';
  $count++ ;
  }
 
$currPage = (($s/$limit) + 1);
 
  echo "<br />";
 
  if ($s>=1) { // bypass PREV link if s is 0
  $prevs=($s-$limit);
  print "&nbsp;<a href=\"search.php?s=$prevs&q=$var\" class='albumname'><< Prev 10</a>&nbsp&nbsp;";
  }
 
  $pages=intval($numrows/$limit);
 
 
  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=\"search.php?s=$news&q=$var\" class='albumname'>Next 10 >></a> ";
  }
 
$a = $s + ($limit) ;
  if ($a > $numrows) { $a = $numrows ; }
  $b = $s + 1 ;
  echo "<p class='numbers'>Showing results $b to $a of $numrows</p>";
  
?><br />
    <br />
 <img src="images/line_divider.gif" width="550" height="1" /><br /><br />
  <span class="orangetitle">Esperan Tutorials...</span>
 <?php
 

Has anybody got any suggestions?
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Search Script

Post by Mirge »

$numresults=mysql_query($query)

Add: or die(mysql_error());

...

$numresults=mysql_query($query) or die(mysql_error());

Find out why the query isn't being executed.
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: Search Script

Post by wolfnipplechips »

Hi,

Thanks for you reply.

This is a the result having searched for the term verilog...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%verilog%" order by courseTitle' at line 1
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Search Script

Post by Mirge »

wolfnipplechips wrote:Hi,

Thanks for you reply.

This is a the result having searched for the term verilog...

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%verilog%" order by courseTitle' at line 1
$query = "select * from course where courseTitle like \"%$trimmed%\ OR courseDescription like \"%$trimmed%\" order by courseTitle";

Look closely at courseTitle... you're missing the ending (closing) double quote.
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: Search Script

Post by wolfnipplechips »

Yep, missing the double quote on the first \"%$trimmed%\"

Thanks for clearing that up.
User avatar
Mirge
Forum Contributor
Posts: 298
Joined: Thu Sep 03, 2009 11:39 pm

Re: Search Script

Post by Mirge »

No worries.
Post Reply