Jump to content

REGEXP not working properly


atom_p

Recommended Posts

This is the first time Im trying to use REGEXP, but cant get it working. It gives no error, just nothing..Anyone got idea why, please? Thanks

 

<?php

include ('connectToDatabase.php');

 

$getSearch = $_REQUEST['getSearch'];

 

$keywordSearchQuery = "SELECT shoeName, shoeSize, colour, price, gender, description WHERE

shoeName REGEXP '.*($getSearch).*' ||

shoeSize REGEXP '.*($getSearch).*' ||

colour REGEXP '.*($getSearch).*' ||

price REGEXP '.*($getSearch).*' ||

gender REGEXP '.*($getSearch).*' ||

description REGEXP '.*($getSearch).*"

;

 

$keywordSearchQueryResult = mysql_query($keywordSearchQuery);

 

echo "$keywordSearchQueryResult";

?>

Link to comment
https://forums.phpfreaks.com/topic/220548-regexp-not-working-properly/
Share on other sites

Case like yours, you probably should use LIKE

so instead of shoeName REGEXP '.*($getSearch).*'

u can use shoeName LIKE '%$getsearch%' but if you still wish to use REGEXP, you can try this

 

shoeName REGEXP '[[:<:]]".$getsearch."[[:>:]]'=1

 

100% must work! hope that helps

There's a strong possibility that the query is failing to execute, but you don't check for that condition. Even if the query succeeds, the $keywordSearchQueryResult variable wouldn't hold a value, it would hold a query result resource. You need to access the result resuorce with the appropriate function, such as mysql_result or one of the mysql_fetch_*() functions.

 

if( $keywordSearchQueryResult = mysql_query($keywordSearchQuery) ) {
   if( mysql_num_rows($keywordSearchQueryResult) > 0 ) {
      while( $array = mysql_fetch_assoc($keywordSearchQueryResult) ) {
         echo '<pre>'; print_r($array); echo '</pre>';  // just prints out the contents of each record. Nothing fancy.
      }
   } else {
      echo 'Query succeeded, but returned 0 results.';
   }
} else {
   echo "<br>Query string: $keywordSearchQuery<br>Produced error: " . mysql_error() . '<br>';
}

 

 

P.S. It would also be helpful to know what you're trying to accomplish . . .

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.