Jump to content


[Solved Finally.] Search engine.


13 replies to this topic

#1 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 24 March 2007 - 10:56 PM

Alright so this code just wont work. I dont get any errors. But it just dosnt search the database. Everything is there and in the correct database and it connects fine but just dosnt search ;)

Help! ;)

MY HTML
			<form method="post" action="index.php?page=pages&act=search_do" class="search">
				<p><input name="search" class="textbox" type="text" />
				  <input name="search" class="searchbutton" value="Search" type="submit" /></p>
			</form>


MY PHP
					<a href="searchresults"></a>
					<h1>Search Results</h1>

				<p>
			<codeblock>
<?php
$search = htmlspecialchars($_POST["search"]);

$result = mysql_query("SELECT * FROM `articles` WHERE subject LIKE '%$search%' AND content LIKE '%$search%' AND postdate LIKE '%$search%'")or die(mysql_error());

if ($search = ""){
	echo 'Please enter a term to search';
}
elseif (mysql_num_rows($result) == "0") {
	echo 'Nothing was found. Try entering another search term.';
}else{

while($row = mysql_fetch_array($result)){

	$aid = $row['aid'];
	$subject = $row['subject'];
	$postdate = $row['postdate'];
	$content = bbcode($row['content']);

   echo "$subject - <a href=\"#\">view</a> | $postdate <br />
";
}
}				
?>
			</codeblock>
				</p>



Anyone have a clue? And yes their on different pages.

Edited by Braunson, 28 March 2007 - 01:49 PM.


#2 Herbert

    Young Padawan

  • Members
  • Pip
  • 35 posts
  • Location:Under Your Bed!
  • Interests:Computers, Rock Music, Cars, Girls!

Posted 25 March 2007 - 02:48 AM

I dont see any thing that sticks out. But i never saw anyone use:

LIKE '%$search%'


#3 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 25 March 2007 - 07:53 PM

Nothing, I get the same thing. Nothing was found. Try entering another search term. everytime I search something. :o

#4 bezz

    Young Padawan

  • Members
  • Pip
  • 23 posts
  • Gender:Male
  • Location:Berlin, NJ
  • Interests:Flash, Snowboarding, Paintball, Learning Guitar

Posted 26 March 2007 - 02:16 AM

How about changing the query to something like this:
$result = mysql_query("SELECT * FROM `articles` WHERE subject LIKE '%$search%' or content LIKE '%$search%'")or die(mysql_error());
I'm pretty sure that's your problem.

Edited by bezz, 26 March 2007 - 02:17 AM.


#5 Mr. Matt

    Moderator

  • P2L Staff
  • PipPipPipPip
  • 1,945 posts
  • Gender:Not Telling

Posted 26 March 2007 - 05:34 AM

I think this is what you are looking for:

$result = mysql_query( "SELECT * FROM `articles` WHERE (`subject` LIKE '%".$search."%' OR `content` LIKE '%".$search."%' OR `postdate` LIKE '%".$search."%')" )or die( mysql_error() );

The query you did won't return anything as all the conditions have to be filled, so if postdata doesn't have anything like $search, but subject and content do, nothing will be returned as all the conditions havn't been met.

I changed bezz's example a bit, because when you do a search with OR's i find it is always best to group them together in the brackets. Not sure if it makes any difference or not, but see what you get and let us know.

Matt

#6 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 26 March 2007 - 09:36 AM

Quote

Awsum, Thank you. Matt. It works. I dont know why I didnt try that. :) :biggrin:


EDIT: Hmm still dosn't work. What could still be worng? I search the stuff that's in the database, but nothing comes up. =\

Also when I just go to the search.php itself it displays the articles without me searching for anything...

Edited by Braunson, 26 March 2007 - 03:14 PM.


#7 bezz

    Young Padawan

  • Members
  • Pip
  • 23 posts
  • Gender:Male
  • Location:Berlin, NJ
  • Interests:Flash, Snowboarding, Paintball, Learning Guitar

Posted 26 March 2007 - 08:47 PM

Found it. Another problem is your html form. You use the name search twice. Change
<input name="search" class="searchbutton" value="Search" type="submit" />
to
<input name="submitsearch" class="searchbutton" value="Search" type="submit" />


#8 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 27 March 2007 - 11:39 AM

Alright now that sorta fixes it accept for when I go to the page, the serch.php itself, It displays everything in the database, how Would I restrict this and display 'Please enter a search term'.

Thanks...

#9 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 27 March 2007 - 03:36 PM

Use a line like this.

if(isset($_POST['submitsearch']) || $_POST['submitsearch'] === 'Search'){
// Execute Search Query
}


#10 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 27 March 2007 - 04:39 PM

Awsum. Thanks everybody. It's almost solved.

Now when i go to the page, it displays 'please enter a serach term' but when i just hit Search button without entering anything in the database it displays all results.

Why is that?
I dont see how it could do that.... :S

Edited by Braunson, 27 March 2007 - 06:22 PM.


#11 SecondV

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Kentucky
  • Interests:All things PHP &amp; MySQL :)

Posted 27 March 2007 - 06:31 PM

Try:

EDIT: has a missing }

<?php

if (!empty($_POST['submitsearch']))
{
	$search = htmlspecialchars(trim($_POST['search']));

	if (empty($search))
	{
		echo 'Please enter a search term. <a href="java script:history.back(-1);">&laquo; Back</a>';
	}
	else
	{
?>
<a href="searchresults"></a>
<h1>Search Results</h1>
<p>
<codeblock>
<?php

		// escape string
		if (function_exists('mysql_real_escape_string'))
		{
			$search = mysql_real_escape_string($search);
		}
		else
		{
			$search = mysql_escape_string($search);
		}

		// Escape for "LIKE"
		$search = str_replace(array('%', '_'), array('\%', '\_'), $search);

		$result = mysql_query("
			SELECT *
			FROM `articles`
			WHERE `subject` LIKE '%$search%'
				OR `content` LIKE '%$search%'
				OR `postdate` LIKE '%$search%'
		") or die(mysql_error());

		if (mysql_num_rows($result) == 0)
		{
			echo 'Nothing was found. Try entering another search term. <a href="java script:history.back(-1);">&laquo; Back</a>';
		}
		else
		{
			while($row = mysql_fetch_assoc($result))
			{
				$aid = $row['aid'];
				$subject = $row['subject'];
				$postdate = $row['postdate'];
				$content = bbcode($row['content']);
				echo "$subject - <a href=\"#\">view</a> | $postdate <br />";
			}
		}
?>
</codeblock>
</p>
<?php
	}
}
?>

pffft, IPB - when using the code remove the space from: java script

Edited by SecondV, 27 March 2007 - 06:47 PM.


#12 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 27 March 2007 - 08:03 PM

Awsum, that works. Now just 1 quick question. How would i make it so I myself could use http://website.com/i...p?page=searchdb&search=TERMHERE

I try'd but nothing....

#13 SecondV

    Young Padawan

  • Members
  • Pip
  • 28 posts
  • Gender:Male
  • Location:Kentucky
  • Interests:All things PHP &amp; MySQL :)

Posted 28 March 2007 - 05:28 AM

<?php

if (!empty($_POST['submitsearch']) OR !empty($_GET['search']))
{
	$search = htmlspecialchars(trim($_REQUEST['search']));

	if (empty($search))
	{
		echo 'Please enter a search term. <a href="java script:history.back(-1);">&laquo; Back</a>';
	}
	else
	{
?>
<a href="searchresults"></a>
<h1>Search Results</h1>
<p>
<codeblock>
<?php

		// escape string
		if (function_exists('mysql_real_escape_string'))
		{
			$search = mysql_real_escape_string($search);
		}
		else
		{
			$search = mysql_escape_string($search);
		}

		// Escape for "LIKE"
		$search = str_replace(array('%', '_'), array('\%', '\_'), $search);

		$result = mysql_query("
			SELECT *
			FROM `articles`
			WHERE `subject` LIKE '%$search%'
				OR `content` LIKE '%$search%'
				OR `postdate` LIKE '%$search%'
		") or die(mysql_error());

		if (mysql_num_rows($result) == 0)
		{
			echo 'Nothing was found. Try entering another search term. <a href="java script:history.back(-1);">&laquo; Back</a>';
		}
		else
		{
			while($row = mysql_fetch_assoc($result))
			{
				$aid = $row['aid'];
				$subject = $row['subject'];
				$postdate = $row['postdate'];
				$content = bbcode($row['content']);
				echo "$subject - <a href=\"#\">view</a> | $postdate <br />";
			}
		}
?>
</codeblock>
</p>
<?php
	}
}
?>


#14 Braunson

    Young Padawan

  • Members
  • Pip
  • 237 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 28 March 2007 - 01:49 PM

Sweeeet, Thanks you so very much.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users