...

View Full Version : PHP Search script problem



yasirjamal
03-07-2010, 08:56 AM
Can anyone please tell me what is the problem on this script getting error on line 20.


<html>
<head>
<title>search script</title>
</head>
<body>

<form name="form" action="search.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("search") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from search where name like \"%$trimmed%\"
order by name"; // 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>";

?>

</body>
</html>

koko5
03-07-2010, 10:00 AM
<html>
<head>
<title>search script</title>
</head>
<body>

<form name="form" action="search.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; // no spaces here
................................You've also to pass $trimmed in mysql_real_escape_string() before quering DB.
Regards :)

yasirjamal
03-07-2010, 12:14 PM
Thank You KOKO

Its working now. But the problem is now. What i want the search result appear in a nice way. Like Name fisrt then discription after that URL. Can you please tell me how i do that. Because know its only showing 1 result found and keyword.

koko5
03-07-2010, 12:57 PM
Hi yasirjamal,

You've to change:

// now you can display the results returned
while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];

echo "$count.)&nbsp;$title" ;
$count++ ;
}

to

while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];

echo "{$count}.)&nbsp;Name:{$row['DBtable_field_name']}&nbsp;{$title}" ;
$count++ ;
}
where "DBtable_field_name" should be renamed to your DB table's column name which holds first name (or you can echo $row[0] ... $row[1] to display any other desired columns. I mean $row can be accessed as associative or non-associative array).

Analogic for any other data you wish to display.
Regards
Edit:I saw now in your code:


// Build SQL Query
$query = "select * from search where name like \"%$trimmed%\"
so field name is "name". Than display should be:

while ($row= mysql_fetch_array($result)) {
$title = $row["1st_field"];

echo "{$count}.)&nbsp;Name:{$row['name']}&nbsp;{$title}" ;
$count++ ;
}

yasirjamal
03-07-2010, 02:33 PM
Thanks alot koko,

Its working fine now. I have few more question and i know u are a professional programmer can solve it very quickly.

1: If you write some wrong keyword on website it will show you sorry your search "keyword" result zero. Its fine i also want if someone type wrong thing it will tell people. But it will not stop there it will also show you the that you search for " keyword" at the bottom site it will show you next link which i dont want if you check my script you will understand better.

2: Pagination link is not working when i click next button it will show the same page again and also i want previous links as well.

3: How i can use css style on php script.

4: Do you think this script is fine to use it on website?

If you can answer me these question it will be a greate help!!


<?php

// Get the search variable from URL

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

// rows to return
$limit = 10; // no spaces here

// 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("search") or die("Unable to select database"); //select which database we're using

// Build SQL Query
$query = "select * from search where name like \"%$trimmed%\"
order by name"; // 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>";
}

// 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 "<b>You searched for: &quot;" . $var . "&quot;</b><br><hr>";

// 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["name"];
$dis = $row["discription"];
$url = $row["url"];

echo "
<b>$title</b><br>
$dis<br>
<a href='$url'>$url</a><br><br>
";

$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=\"search.php?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=\"search.php?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>";

?>

koko5
03-07-2010, 05:16 PM
Hi yasirjamal, sorry for the delay,

Of course you can use css, simply include('yourfile.css'); in header section of your page. I think this code can be used, but please check also professional solutions , provided and tested by master coders:

this class (http://codingforums.com/showthread.php?t=74614)
and this thread (http://codingforums.com/showthread.php?p=884187) too

You can find great information about pagination links generation and securing your script from sql injections.
Finding answers yourself (to teach fishing) is the best option.
Thanks :thumbsup:
p.p.: searching this forum you can find ready-to-use solutions, which have more than desired functionality and post questions to the authors.
I didn't post all interesting links.

_Aerospace_Eng_
03-07-2010, 06:09 PM
4: Do you think this script is fine to use it on website?

I'll answer #4. If you want your site hacked then yes use this script on a website. If don't want it hacked then I suggest you look into mysql injection to learn what it is and how to prevent it.

http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php

yasirjamal
03-08-2010, 08:39 AM
Thank koko, Aerospace_Eng.

I am new to php can you please refer me a good search engine script to search my database. A secure script with pagination option

Thanks alot

yasirjamal
03-08-2010, 08:39 AM
how to delete my posts



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum