Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
SQL search function problem
Old 11-28-2007, 12:29 AM SQL search function problem
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
Hey all,
I tried searching for this but I couldnt find anything so here goes nothing.

Im trying to make a search feature that will search through a sql db. I want to let users input a field and then have it check against the info in the 'name' field. Heres what i have so far, any suggestions would be great!

PHP Code:
<?php 
$searching 
=="yes";
$find=="Flight of Fury";
include 
"variables.php";
echo 
"search1";
//This is only displayed if they have submitted the form 
if ($searching ="yes"

echo 
"<h2>Results</h2><p>"

//If they did not enter a search term we give them an error 
if ($find ""

echo 
"<p>You forgot to enter a search term"
exit; 


// We preform a bit of filtering 
$find strtoupper($find); 
$find strip_tags($find); 
$find trim ($find); 

//Now we search for our search term, in the field the user specified 
$result mysql_query("SELECT * FROM mreleased WHERE name LIKE '%$find%'") or die(mysql_error()); 

//And we display the results 
while($row mysql_fetch_array$result )) 

echo 
$row['name']; 
echo 
" "
echo 
$row['date']; 
echo 
"<br>"
echo 
$row['genre']; 
echo 
"<br>"
echo 
"<br>"


//This counts the number or results - and if there wasnt any it gives them a little message explaining that 
$anymatches=mysql_num_rows($result); 
if (
$anymatches == 0

echo 
"Sorry, but we can not find an entry to match your query.<br>Notice a problem with our search? Please contact us about it, or add this movie to the database to help our site continue to grow and improve.<br><br>"


//And we remind them what they searched for 
echo "<b>Searched For:</b> " .$find
}
?>
Basically the part that (I think) is giving me problems is the sql query, when I run this it just outputs a whole but of items and not the one I have selected.

Any ideas would be greatly appreciated.
Truly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-28-2007, 05:18 AM Re: SQL search function problem
Super Talker

Posts: 134
Trades: 0
$searching ="yes";
$find="Flight of Fury";
include
"variables.php";
echo
"search1";
//This is only displayed if they have submitted the form
if ($searching =="yes")
{
echo
"<h2>Results</h2><p>";

//If they did not enter a search term we give them an error
if ($find == "")
{
echo
"<p>You forgot to enter a search term";
exit;
}

you mix up = and ==
__________________

Please login or register to view this content. Registration is FREE

Check out the Facebook Clone build with Jcow SNS at
Please login or register to view this content. Registration is FREE
, it is free and it always will be
Falcone is offline
Reply With Quote
View Public Profile
 
Old 11-28-2007, 05:56 AM Re: SQL search function problem
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
As Falcone said...
To sum it up:
= does an assignation of a value to a variable
PHP Code:
x=//$x contains the value 1 
== does a test and return true or false depending the result of the test
PHP Code:
1==1  //returns true
1==2  //returns false 
=== does a test like ==, but check if the type is the same too.
PHP Code:
1==='1'//return false: check between a numeric and a string value. == returns true, because the numeric value is casted as a string value implicitly 
__________________
Only a biker knows why a dog sticks his head out the window.

Last edited by tripy; 11-28-2007 at 05:57 AM..
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 11-29-2007, 01:52 AM Re: SQL search function problem
Truly's Avatar
Ultra Talker

Posts: 322
Trades: 0
You guys are awesome, it never fails when I have a problem this site helps me out!

On that note though :P, I have another problem. I thought the %LIKE% feature in sql was supposed to look for similar terms.

PHP Code:
$result mysql_query("SELECT * FROM mreleased WHERE name LIKE '%$find%'") or die(mysql_error()); 
This is for a movie database thing Im working on just to get more experience with PHP. So when I set $find="Bobby Z" it works fine, but if I do "Bobby-Z" or "Boby Z" it returns back nothing found.

Is there a different function I should be using instead of %LIKE%?

Thanks guys
Truly is offline
Reply With Quote
View Public Profile
 
Old 11-29-2007, 12:50 PM Re: SQL search function problem
Arenlor's Avatar
Ultra Talker

Posts: 462
Name: Jerod Lycett
Location: /home/arenlor
Trades: 0
http://dev.mysql.com/doc/refman/5.0/...-matching.html
You'd have to do something severely system intensive to get it to work with ANY search pattern, but what you could do is, and it would take too long to actually write the code for this but, basically you are going to take the $find and before the query you'll take and sanitize it somewhat, you'll take all letters and make it lower case then you'll turn all hyphens and whatever else you want to allow into spaces and all repeat letters (boBBy) into a single letter (boby). So all three of your searches above would turn into "boby z". That's all the further I'm SURE of how to do, the next part I'm not, but basically in the query you're going to do something like:
PHP Code:
$result mysql_query("SELECT * FROM mreleased WHERE name REGEXP '$regexp'") or die(mysql_error()); 
To get $regexp is the part I don't know fully since it'd take arrays, but you'd make an array of the characters of the new $find, "boby z" in our case. Then you'd pull a foreach() on the array making a new array $newarray[] and incrementing a number $i to indicate how many letters are in your array, each part of the new array would be "[$char|$CHAR]+" which if echoed would be [b|B]+ which should allow b, B, BB, bbbbbbb, etc but not bB nor Bbbbbbbbbb but for spaces you would just have it be [ ]+. You will then set $regexp = "^".$newarray[0].$newarray[1].$newarray[2]..."$'; so that all together $regexp would echo as, in our case ^[b|B]+[o|O]+[b|B]+[y|Y]+[ ]+[z|Z]+$ this should match what you're looking for.
__________________
PHP Code:
<?php echo "Hello World"?>
HTML Code:
<html><head><title>Hello World</title></head><body><p>Hello World</p></body></html>
Arenlor is offline
Reply With Quote
View Public Profile Visit Arenlor's homepage!
 
Old 11-29-2007, 05:02 PM Re: SQL search function problem
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
If you are using mysql and your site is in English, try the soundex algorithm.
I don't know about performances, but this could help you.
http://dev.mysql.com/doc/refman/5.0/...nction_soundex
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to SQL search function problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.92670 seconds with 12 queries