help making simple search function

Man of Honour
Joined
12 Jan 2003
Posts
20,633
Location
UK
was wondering if anyone might be able to offer some advice. I'm looing for a very simple way of adding a database search to my site. just a simple text box so that when you enter a word in it, it seaches some form of database and returns any matching results. sounds fairly simple, but i cant get it to work (i am not very experienced with web design at all!)

main thing is that i need it to search a really easy to maintain database, maybe even something like an excel sheet, or even a text doc if thats possible?! need to be able to update the entries in it a lot thats all.

is there any chance somone can put together a VERY simple example for this? perhaps a simple html coded search page and an example results page? just something with a "search" page and a database which it can search?! once i see how things like this work i find it easy enough to pick it up and edit it etc, just need a kick start! doesnt matter how simple it is!

any help is most appreciated :)

thanks
 
Hi,

Here's some, I've just tested it on my site & it works - make sure the field you're searching is fulltext else it'll give an error.

It's not safe, I can safen it up for you if you want though?

Here's the code:
Code:
<?PHP
// Include the file with the connection details in it.
include 'includes/connection.php';

$Query = $_POST['Query'];

// Do the search query
$sql = "SELECT * FROM FieldName WHERE MATCH (Field_You're_Searching) AGAINST ('$Query')";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) 
{
    $QueryResults = $row['FieldName'];
    
// Print the search results
    echo ' '.     $QueryResults.' ';
}

// Include the file with the mysql close connection code, not needed.
include 'includes/closeconn.php';
?>

Edit:

Oh and here's the form, you'll need to change search_submit.php according to what you've named the file that contains the code above.

Code:
<form name="form1" method="post" action="search_submit.php">
  <input name="Query" type="text" id="Query">
  <input type="submit" name="Submit" value="Submit">
</form>

Craig.
 
Last edited:
Craig, that's fantastic, i'll give it a go and see how i get on :)

edit: the first bit of code there, is that where i also need to include the results? or do i do another file containing all the results?

for example, if i wanted to have a database listing loads of fruit (i don't, it's just an example! :p) where would i put all the entries, and what would they need to look like?
 
Well that script will print all the results it has found in the database anyway via this line:

Code:
echo ' '.     $QueryResults.' ';

Obviously you're gonna need a back end MySQL database with all the information but don't forget to change the SQL query in the code above to the table names in your database :)

EDIT: Oh and you will need a file called 'connection.php' to connect to the database with something like this in:

Code:
<?php

$dbuser = "username";
$dbpass = "password";
$dbhost = "localhost";
$dbase = "database1";

$connection = mysql_connect($dbhost, $dbuser, $dbpass);

mysql_select_db($dbase) or die('Could not find selected database');

?>

and then just replace the lines:

Code:
// Include the file with the mysql close connection code, not needed.
include 'includes/closeconn.php';

with

Code:
// Close Connection:
mysql_close($connection);

:)
 
Last edited:
thanks Trigger. I havent really dealt with SQL databases either, and i'm grateful if people can "hold my hand" through it :) Can this search function above be used to link into any other type of database? like even a text doc in a certain format? Any chance someone could send me (email in sig) and example SQL really simple database and point me in the right direction.

i wish i knew more about all this stuff! :)
 
Baddass said:
thanks Trigger. I havent really dealt with SQL databases either, and i'm grateful if people can "hold my hand" through it :) Can this search function above be used to link into any other type of database? like even a text doc in a certain format? Any chance someone could send me (email in sig) and example SQL really simple database and point me in the right direction.

i wish i knew more about all this stuff! :)

I think that can only be used for MySQL databases and to search text files would be a nightmare and although do-able, would be really difficult to manage :( You don't need to be able to code SQl to make a database- you can do it all through PhpMyAdmin and you can even add values via this interface. It would probably be easier though to create a password protected form to allow you to add things to the database- I'll see if I can write you something up after lunch :)
 
that sounds fantastic, i would be very grateful for any help you can give. Like i say, i just need a starting block really, reading through the code it makes sense to me, i just wouldnt know where to begin. if you could put an example together that would be brilliant
 
I would reccomend this book - http://www.amazon.co.uk/exec/obidos...697/sr=1-23/ref=sr_1_2_23/203-7319550-2300761

It's only a couple of hundred pages at most and guides you through the very basics of PHP and then how to install MySQL and how to insert and retrieve data from the database. You'll probably need another resource after you've got all the basics down but it really spells it out for you. It's almost foolproof.
 
Baddass said:
that sounds fantastic, i would be very grateful for any help you can give. Like i say, i just need a starting block really, reading through the code it makes sense to me, i just wouldnt know where to begin. if you could put an example together that would be brilliant

I've sent the files to your ocukmods.co.uk e-mail address in ZIP format. Hope they're of some use to you :)

Ben
 
Trigger said:
I've sent the files to your ocukmods.co.uk e-mail address in ZIP format. Hope they're of some use to you :)

Ben

Ben, thank you for all your help. I'll have a look through them now :)
 
It all alright now Baddass? Sorry I didn't reply again, I kinda forgot & have been busy.

I can make it nice and secure for you if you're going to be using it publicly?

Craig.
 
Back
Top Bottom