|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
mysql error for multiple keywords
This is the code im working with:
------------------------------------------ <?php $location = 'localhost'; $username = 'username'; $password = 'password'; $database = 'cats'; $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $keywords= $HTTP_POST_VARS["words"]; $pieces = explode (" ", $keywords); $count=count($pieces); $query="SELECT * FROM links WHERE"; if ($count==1){ $query.="LIKE '%$keywords%'"; } elseif($count>=1){ for($a=0;$a<$count;$a++){ $query.="field LIKE '%$pieces[$a]%'"; if($a != $count){ $query.="OR"; } } } $result=mysql_query($query,$conn) or die("MySQL Error: ".mysql_errno().": ".mysql_error()); $numOfRows = mysql_num_rows($result); for($i = 0; $i < $numOfRows; $i++) { $id = mysql_result ($result, $i, "id"); $cat_id = mysql_result ($result, $i, "cat_id"); $url = mysql_result ($result, $i, "url"); $title = mysql_result ($result, $i, "title"); $desc = mysql_result ($result, $i, "desc"); $keywords = mysql_result ($result, $i, "keywords"); echo "<a href='$url'>$title</a><BR>"; echo "$desc<BR>"; } print $query; ?> ------------------------------------------------ This is the error i get searching a single word like "search" in the text box: ------------------------------------------------ MySQL Error: 1064: You have an error in your SQL syntax near ''%search%'' at line 1 ------------------------------------------------ This is the error i get searching 2 words like "earch keywords2" in the text box: ------------------------------------------------ MySQL Error: 1064: You have an error in your SQL syntax near 'LIKE '%search%'ORfield LIKE '%keywords2%'OR' at line 1 ================================================ What did i do wrong, or what do i need to do,, not only that,, for if someone types a "," inbetween the words in the text box (ie: like "search, keywords2") do i add this to the code? $pieces = explode (",", $keywords); |
|
#2
|
||||
|
||||
|
RE: mysql error for multiple keywords
It looks like your missing spaces before field names. Also, if $count == 1, there is no field specified.
Add $query to the die() statement to see the whole query rahter than just the error. |
|
#3
|
|||
|
|||
|
RE: mysql error for multiple keywords
ok,,yea, i noticed that,,
i now have $query="SELECT * FROM links WHERE "; if ($count==1){ $query.="keywords LIKE '%$keywords%'"; } elseif($count>=1){ for($a=0;$a<$count;$a++){ $query.="keywords LIKE '%$pieces[$a]%'"; if($a != $count){ $query.=" OR "; } } } and now returning this error,,, MySQL Error: 1064: You have an error in your SQL syntax near '' at line 1 i put print "$query"; into the code so it can tell me what it says, but with this mysql error, it wont let the rest of the code work,, this is totally confusing me,,lol.. this is all the code i have now: <?php $location = 'localhost'; $username = 'username'; $password = 'password'; $database = 'cats'; $conn = mysql_connect("$location","$username","$password"); if (!$conn) die ("Could not connect MySQL"); mysql_select_db($database,$conn) or die ("Could not open database"); $keywords= $HTTP_POST_VARS["words"]; $pieces = explode (" ", $keywords); $count=count($pieces); $query="SELECT * FROM links WHERE "; if ($count==1){ $query.="keywords LIKE '%$keywords%'"; } elseif($count>=1){ for($a=0;$a<$count;$a++){ $query.="keywords LIKE '%$pieces[$a]%'"; if($a != $count){ $query.=" OR "; } } } $result=mysql_query($query,$conn) or die("MySQL Error: ".mysql_errno().": ".mysql_error()); $numOfRows = mysql_num_rows($result); for($i = 0; $i < $numOfRows; $i++) { $id = mysql_result ($result, $i, "id"); $cat_id = mysql_result ($result, $i, "cat_id"); $url = mysql_result ($result, $i, "url"); $title = mysql_result ($result, $i, "title"); $desc = mysql_result ($result, $i, "desc"); $keywords = mysql_result ($result, $i, "keywords"); echo "<a href='$url'>$title</a><BR>"; echo "$desc<BR>"; } print "$query"; ?> |
|
#4
|
|||
|
|||
|
RE: mysql error for multiple keywords
Print the $query before that mysql_query().
|
|
#5
|
|||
|
|||
|
RE: mysql error for multiple keywords
ok, ic,,, now im getting the same error, but the $query is putting an extra "OR" at the end of the query...
SELECT * FROM links WHERE keywords LIKE '%search%' OR keywords LIKE '%keywords2%' OR Thats more than likely my prob,,, i hope,, now,, how to get rid of that..lol MySQL Error: 1064: You have an error in your SQL syntax near '' at line 1 |
|
#6
|
|||||
|
|||||
|
RE: mysql error for multiple keywords
You should use if($a != $count-1) instead of if($a != $count).
Or, you could put everything in an array and then use join(). php Code:
Or, a little more cryptic, but even less code: |
|
#7
|
|||
|
|||
|
RE: mysql error for multiple keywords
THANK YOU GREATLY,,,,!!!!
Ashkhan honcho Nobody realizes that even a lil piece of code like that, and people like you both,,, are the PHPGODS...lol.. I have actually been working on that one piece of code for almost 3 months now trying to figure out how to have multiple keywords used in a search text,, now i have Categories, SubCategories, Multiple keyword search, and now,, one of two things..lol either a spider, or a meta spider,, now, one question,,, useing "," comma's in the text like the space in the above code,,, can i just put : $pieces .= explode (",", $keywords); the actual code is: $pieces = explode (" ", $keywords); for the space or do i have to do it another way? cuz ive notice that i use the comma alot when i search for things,, |
|
#8
|
||||
|
||||
|
RE: mysql error for multiple keywords
You can split on both space and comma (and make sure you have no empty pieces at the same time):
|
|
#9
|
|||
|
|||
|
RE: mysql error for multiple keywords
That is so cool...
what would a coder do without you guys and codewalkers forums, i have to get an account..lol thanks alot honcho.. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Database Help > mysql error for multiple keywords |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|