Jump to content

Need help searching my database


Accurax

Recommended Posts

2 tables in my database, and i need to create a function that will allow a user to search these tables and then display the results in a given order.

I'm having serious trouble finding a tutorial on how to achieve this ...... please, does anyone know of a half decent tutorial that i can follow as i could really do with learning how to achieve this asap.

If you could post any links you have id really really appreciate it... thankuyyou guys
Link to comment
https://forums.phpfreaks.com/topic/33514-need-help-searching-my-database/
Share on other sites

Well, your description is a little vague.

here are some tutorials that may or may not have what you are looking for:
http://www.tizag.com/mysqlTutorial/mysqlquery.php
http://www.developerfusion.co.uk/show/3998/7/

If you were to describe the format of your tables and how you want to search them and wheat data you want retrieved., I think we could probably provide the code as well as an explanation of how it works.
create a search form. Pass the data to a sql query

$query="SELECT * FROM table1, table2 WHERE field1, field2 LIKE '%$searchvar%';

this query will select all from the 2 tables where field1 or field 2 is like the string entered in the search box.



The % is a wildcard character. %abc% will find 123abc456def , %abc will find 123abc, abc% will find abc456def

assuming there is a string in the DB like this  123abc456def
Thanks guys,

Heres a little more detail ..

I have two tables, one holds personal information about a member, and the other holds a link to their avatar, now, i want to create quite a detailed form that allows a user to select various options from drop down menu's.. as well as a keyword match to an individuals profile.

I want the script to take the fields selected and match them to users in the database, for example

SELECT * FROM users WHERE age=26 && musictaste=opera && .... && .. && etc etc

however, i dot want the entire form to have to be completed, id like the script to simplt ignore form fields that were not changed from the dafault, thus allowing a user to tailor their own search parameters.

And as i mentioned, id like a full text search, that matched entered strings to any text that appears in several given fields in my table.

Then i want to display these results on a webpage... I understand that i need to use LIMIT in order to get results showing accross several pages, and so i need to look into this.

As far as results display goes... its simply the avatar and the username linking to each users profile.
Well...

When the form is submitted youwill just need to parse the field data to build your query string based upon the fields that have data. Here is one possible example:
[code]$sql = "SELECT * FROM users WHERE ";
if ($_POST['age']) { $whereAry[] = "age = ". $_POST['age']; }
if ($_POST['musictaste']) { $whereAry[] = "musictaste = ". $_POST['musictaste']; }
//etc.
$whereClause = implode(" AND ", $whereAry);
$sql .= $whereClause;[/code]

For full-text searching and pagination take a look at the tutorials on this site:
[url=http://www.phpfreaks.com/tutorials/129/0.php]MySQL Full-Text Searching with PHP[/url]
[url=http://www.phpfreaks.com/tutorials/147/0.php]Expanded Pagination[/url]

Archived

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

×
×
  • 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.