Jump to content
xisto Community
Sign in to follow this  
Amezis

Searching In Mysql

Recommended Posts

Well, I am creating a search script for a MySQL-database. But how can people submit "first second", and then the script searches for both of the words, not the exact phrase?

So if someone types "first second" it will also show entries containing "first blablabla second".

This is basically the code I am using:

SELECT * FROM `$table` WHERE `row` LIKE '$text'
But this only shows the exact phrase :)

Share this post


Link to post
Share on other sites

I'm guessing that $text = 'First Second'...

You need to insert some wild cards, like this

$search_text = '%' . str_replace(' ', '%', $text) . '%';

and then do:

SELECT * FROM `$table` WHERE `row` LIKE '$search_text'

this should search for everything that includes the two words...

You might need to put some extra spaces in the $searc_text...

Share this post


Link to post
Share on other sites

Here's what I've understood. You are trying to search for the database entry that contains the two or three words rather than the words being combined together.

You will need to use the wildcards in the LIKE and WHERE function in the SQL statements.

Here's some examples:

The following SQL statement will return persons with first names that start with an 'O':

SELECT * FROM Persons WHERE FirstName LIKE 'O%'

The following SQL statement will return persons with first names that end with an 'a':
SELECT * FROM Persons WHERE FirstName LIKE '%a'

The following SQL statement will return persons with first names that contain the pattern 'la':
SELECT * FROM Persons WHERE FirstName LIKE '%la%'

Share this post


Link to post
Share on other sites

if you just want to use PHP and want to parse the whole DB try something this:

<?PHP$search="search";mysql_connect("localhost","root","yourpass");mysql_select_db("namelist")$res=mysql_query("SELECT * FROM `Persons`");$i=mysql_num_rows($res);while($data=mysql_fetch_array($res)){if(!$data['FirstName']==$search){										 ##if NOT firstname=search##echo "no";}else{echo $data['ID'];}}or you can let the string explode per space like this:(name = "This Name")$text=explode(" ",$name);for($i2="0";$i2<"2";$i2++) {echo $text['$i2'];}

if you use a session, this will not be touched, 'cause the exploded text will be stored in the array named text


i hope, this will help you :)
Notice from jlhaslip:
Code tags are required for these sample snippets. Thanks.

Share this post


Link to post
Share on other sites

Ok, I got another question now. How can I search multiple rows if the user submits one field?So, if the user submits something in a field named "fieldname", it will first check the first row (row1). If there's no hits for that search, it goes to the next row (row2). If there's no hits, it goes to row3 etc.

Share this post


Link to post
Share on other sites

Bumping.

I also want this to be in ADDITION to the other fields.

Basically, this is what the search form would look like:

|First field|	 |Second Field|		   [submit]
The results from the first fields will be what I asked above. It works fine now. The second field will search in 3 mysql rows: Row1, row2, row3. The rest is explained above. How do I do that? I can't manage to make it work for multiple rows...

Share this post


Link to post
Share on other sites

I Write this tut For You
At first Make one file with name : Search.htm and then Copy this code and paste in to :

1-

<form method="post" action="search.php"><input type="text" name="search" size=25 maxlength=25><input type="Submit" name="Submit" value="Submit"></form>
Above code is a form point to search.php file
above code has one textbox and one buttom

2 - open Your Notepad and Copy this code into :
<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","password");//select which database you want to editmysql_select_db("local_news");$search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){//the format is $variable = $r["nameofmysqlcolumn"];//modify these to match your mysql table columns$title=$r["title"];$message=$r["message"];$who=$r["who"];$date=$r["date"];$time=$r["time"];$id=$r["id"];//display the rowecho "$title <br> $message <br> $who <br> $date | $time <br>";}?>

I write This Code 4 make one blog
At end save this code with name : search.php
---------------------------------------------------------------------------
look at this line :
$result = mysql_query("SELECT * FROM news WHERE message LIKE '%$search%'");
in above line you can see LIKE , we user LIKE for Finding more result in SQL
For Example :
if i have 2 raw content farsiscript and farsi and then search farsi
if user $result with LIKE My Search Result show me 2 raw
but if i user $result with out LIKE Result show me 1 raw
----------------------------------------------------------------
look at these lines :
$title=$r["title"];
$message=$r["message"];
$who=$r["who"];
$date=$r["date"];
$time=$r["time"];
$id=$r["id"];
they are variables
----------------------------------------------------------------
look at this line :
echo "$title <br> $message <br> $who <br> $date | $time <br>";
taskwork of echo function is writeing in page
----------------------------------------------------------------
look at this line , its a loop
while($r=mysql_fetch_array($result))
{
if number of $r = number of $result then loop finished
---------------------------------------------------------------
for user this code you must make one database with columns : title , message , who , date , time , id

Share this post


Link to post
Share on other sites

Huh? I know how to do basic searching and how to use the wildcards, look at the above posts. Your tutorial is completely useless, I know everything there, sorry. It wasn't my question at all, please look at my question first :blink:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.