Database Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesDatabase Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old August 11th, 2003, 02:56 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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);

Reply With Quote
  #2  
Old August 11th, 2003, 03:32 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
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.

Reply With Quote
  #3  
Old August 11th, 2003, 03:54 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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";
?>

Reply With Quote
  #4  
Old August 11th, 2003, 04:46 PM
Ashkhan Ashkhan is offline
Contributing User
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 372 Ashkhan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 24 m 57 sec
Reputation Power: 2
RE: mysql error for multiple keywords

Print the $query before that mysql_query().

Reply With Quote
  #5  
Old August 11th, 2003, 06:06 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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

Reply With Quote
  #6  
Old August 11th, 2003, 06:22 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
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:
Original - php Code
  1.  
  2. for($a=0;$a<$count;$a++)
  3. {
  4.    $where[] = "keywords LIKE '%$pieces[$a]%'";
  5. }
  6. $query .= join(' OR ', $where);

Or, a little more cryptic, but even less code:
php Code:
Original - php Code
  1.  
  2. $query .= "keywords LIKE '%".join("%' OR keywords LIKE '%", $pieces)."%'";


Reply With Quote
  #7  
Old August 11th, 2003, 06:59 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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,,

Reply With Quote
  #8  
Old August 11th, 2003, 08:14 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 3
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):
php Code:
Original - php Code
  1.  
  2. $pieces = preg_split("/[, ]+/", $keywords, -1, PREG_SPLIT_NO_EMPTY);


Reply With Quote
  #9  
Old August 11th, 2003, 08:37 PM
Anonymous Anonymous is offline
Registered User
Codewalkers God 35th Plane (22000 - 22499 posts)
 
Join Date: Apr 2007
Posts: 22,309 Anonymous User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 24
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..

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesDatabase Help > mysql error for multiple keywords


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT