View Full Version : Searching Database for Item
unicorn
04-17-2007, 04:32 PM
Hi, I'm a total newbie at this so any help will be greatly appreciated...
Ok, heres what I wanna do:
I want to place an Input textbox on my Index with a submit button to search for details In my Database and display them In a webpage...
DB table name: nk_table
table fields: id, firstname, lastname, address, phone, mobile, email
I want to display full details whenever I search for something...
E.g: when I search for firstname I want the other Info (lastname, address, phone...) to display as well...
So, Is there such a code or script?
Thanx In Advance.
boxxertrumps
04-17-2007, 05:11 PM
Please provide a more descriptive title, Things along the lines of "help/script needed" or "problem" are to vauge.
Mysql:
SELECT * FROM nk_table WHERE MATCH (*) AGAINST (keyword);
That searches the entire table for the keyword...
unicorn
04-17-2007, 05:21 PM
sorry about the title, 'n thanx for ur reply ...
But, how do I use that code!! :confused:
heres my code:
<?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("hidden","hidden","hidden"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("hidden") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "SELECT * FROM nk_table WHERE firstname LIKE \"%$trimmed%\";
order by firstname"; // 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: "" . $trimmed . "" 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: "" . $var . ""</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["1"];
echo "$count.) $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 " <a href=\"$PHP_SELF?s=$prevs&q=$var\"><<
Prev 10</a>  ";
}
// 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 " <a href=\"$PHP_SELF?s=$news&q=$var\">Next 10 >></a>";
}
$a = $s + ($limit) ;
if ($a > $numrows) { $a = $numrows ; }
$b = $s + 1 ;
echo "<p>Showing results $b to $a of $numrows</p>";
?>
boxxertrumps
04-17-2007, 05:47 PM
USE {PHP} BB TAGS.
<?php
...
//connect to your database ** EDIT REQUIRED HERE **
mysql_connect("hidden","hidden","hidden"); //(host, username, password)
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("hidden") or die("Unable to select database"); //select which database we're using
// Build SQL Query
$query = "SELECT MATCH (*) AGAINST (\"%$trimmed%\") AS score FROM nk_table WHERE MATCH (title,body) AGAINST (\"%$trimmed%\");
order by score"; // 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
...
?>
Changed the query to display by relevancy, may or may not work.
For future reference:
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
unicorn
04-17-2007, 06:15 PM
Doesn't work ... I keep gettin'
Results
Sorry, your search: "keyword" returned zero results
Click here to try the search on google
Couldn't execute query
boxxertrumps
04-17-2007, 09:08 PM
for some reason the line with this:
$numrows=mysql_num_rows($numresults);
Was commented.
Fixed the code.
unicorn
04-18-2007, 02:30 PM
ahh, one more thing ...
shall I leave the (*) and (title,body) like that? Or shall I replace them with the fields name?
Sorry 4 the disturbance but as I said I'm a total newbie at this.
thetestingsite
04-18-2007, 02:52 PM
Yes, on this line you need to edit the parts in red:
$query = "SELECT MATCH (*) AGAINST (\"%$trimmed%\") AS score FROM nk_table WHERE MATCH (title,body) AGAINST (\"%$trimmed%\");
order by score";
to match your database table structure.
Hope this helps.
unicorn
04-18-2007, 03:50 PM
Looks like It isn't meant for this to work :mad:
everytime I get a different stupid error, like:
Parse error: parse error, unexpected $ in /var/www/html/search.php on line 102
The funny thing is that I only have "?>" << this code on line 102 which is the end of the script :D
mdcloud
04-30-2007, 04:46 PM
look at the line above that and see if you left out a ;
could be the problem
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.