Hi guys,

I'm implementing a feature on my site were you can search a database and it will bring back results on a page.

I'm not too good with programming as I'm just a casual webmaster so I'm using this tutorial: http://www.designplace.org/scripts.php?page=1&c_id=25

The problem is everytime I click submit it just brings up the text: "Please enter a search..."

I think the problem lies at the database select info. The database contains one table called ClassNotes which holds 55 records.

This is what I used:


  // Get the search variable from URL

  $var = @$_GET['q'] ;
  $trimmed = trim($var); //trim whitespace from the stored variable

// rows to return

// check for an empty string and display a message.
if ($trimmed == "")
  echo "<p>Please enter a search...</p>";

// check for a search parameter
if (!isset($var))
  echo "<p>We dont seem to have a search parameter!</p>";

//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("localhost","alumni_alumni","alumni"); //(host, username, password)

//specify database ** EDIT REQUIRED HERE **
mysql_select_db("alumni_alumni") or die("Unable to select database"); //select which database we're using

// Build SQL Query  
$query = "select * from ClassNotes where 1st_field like \"%$trimmed%\"  
  order by 1st_field"; // EDIT HERE and specify your table and field names for the SQL query


// 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)) {

// 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
  print "&nbsp;<a href=\"$PHP_SELF?s=$prevs&q=$var\">&lt;&lt; 
  Prev 10</a>&nbsp&nbsp;";

// calculate number of pages needing links

// $pages now contains int of pages needed unless there is a remainder from division

  if ($numrows%$limit) {
  // has remainder so add one page

// check to see if last page
  if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

  // not last page so give NEXT link

  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>";

Any help would be great

Recommended Answers

All 7 Replies

Member Avatar for rajarajan2017
$var = @$_GET['q'] ;
  echo $var;

echo your $var variable to know whether you getting the value when you submit. I thought it is not coming.

Also check after submit, you have the url with the query string follwed by quesiton mark.

I added that line and nothing changed.

I'm not sure how to do the second part though - this is just a personal site, I'm not really a web technical person.

I uploaded the files I was using here: www.dndiscounts.info/search.zip


Member Avatar for rajarajan2017

Your index file action attribue value should look like below:

<form  method="post" action=[B]"search.php?q=10"[/B]  id="searchform"> 
 <input  type="text" name="name"> 
<input  type="submit" name="submit" value="Search"> 

Now test it.

Now I get this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/username/public_html/search/search.php on line 44


Member Avatar for rajarajan2017

Change like below to know the error in query

$numresults = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

Error in query: select * from ClassNotes where 1st_field like "%10%" order by 1st_field. Unknown column '1st_field' in 'where clause'

I'm not sure how to select the table from the database

http://i34.tinypic.com/64pi13.png is the structure of the database

Member Avatar for rajarajan2017

Click the structure tab and select your table to know the fieldname. Actually there is no field with the name 1st_field, it may be wrong. I think the field will not start with a digit.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.