Jump to content

General question


Gruzin

Recommended Posts

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.
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.