Jump to content

Archived

This topic is now archived and is closed to further replies.

Gruzin

General question

Recommended Posts

hi guys,
I have a very simple question:
I want to make a search engine for my site, can u tell me wich is the best way to do this? I don't know how to do that, your ideas will be very helpfull for me, thanks.

Share this post


Link to post
Share on other sites
If your whole site is in a databse, like MySQL, then a search if a very easy thing to implement. If your site is in html files, you can create your own search script in php, but depending on how you do it it might be slow, and might not be the best way of doing things.

If you're looking for a super easy solution, you can use Google.. they have a feature that you can use that will allow users to search YOUR site.. you might want to check into that.

Share this post


Link to post
Share on other sites
Google, that requires the site to be in googles database.
which is quite not that easy

Share this post


Link to post
Share on other sites
Well if the site has been online for a couple of weeks, it's sure to be in google. Google is usually pretty fast at that.

Share this post


Link to post
Share on other sites
no, my sites up for months and yet its not in google database

Share this post


Link to post
Share on other sites
ok guys, here is my simple search script, but it doesn't work...
any ideas? thank you for your comments.

[color=red]<?php
$con = mysql_connect("localhost","3d","password");
if(!con){
  die('Can not connect:'.mysql_error());
}
mysql_select_db("3d",$con);
$result = mysql_query("SELECT*FROM search WHERE text");
while($row = mysql_fetch_array($result))
if($result == '$_POST[search]')
  echo $row['text'];
else
  echo "sorry";
mysql_close($con);
?>[/color]

Share this post


Link to post
Share on other sites
Regarding your query:

[code=php:0]
$result = mysql_query("SELECT*FROM search WHERE text");
[/code]

What data is contained in the "search" table?

You specify "WHERE text", but you don't tell MySQL what to check "text" against. This would only work if you did something like:

[code=php:0]
$result = mysql_query("SELECT * FROM search WHERE text = '$somevar'");
[/code]

What you'll need to do is something like this. Start by mapping out what tables in the database you want to search. For example, let's say your website has news articles, with the following database structure:

[b]news_table[/b]
news_id
news_title
news_text

You would want to structure your query simiarl to this:

[code=php:0]
$sql = "SELECT * FROM news_table
          WHERE news_title LIKE '%".$_POST['search']."%'
          OR news_text LIKE '%".$_POST['search']."%'";
$result = mysql_query($sql);
[/code]

You would need to perform a "LIKE" statement for each field in the database you want to check.

Share this post


Link to post
Share on other sites
ok, but how can Compare that to the inputed text from the form?

Share this post


Link to post
Share on other sites
The query I wrote already compares the database to the search text ([code=php:0]$_POST['search'][/code]), and it only returns the matches.

There's no need to do any other comparison, all you have to do from that point is print out the results.

Share this post


Link to post
Share on other sites
ok, thanks I've done it but when I try to print the result it says: [color=green]Resource id #3[/color] - what does that mean?
here is the code:
[color=red]
$sql = "SELECT * FROM search
          WHERE text LIKE '%".$_POST['search']."%'
          OR news_text LIKE '%".$_POST['search']."%'";
$result = mysql_query($sql);
echo $result;
mysql_close($con);[/color]

Share this post


Link to post
Share on other sites
[quote]but when I try to print the result it says: Resource id #3 - what does that mean?[/quote]
It means you should read the manual to find out what the [url=http://php.net/mysql_query]mysql_query[/url]() function returns.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.