Karyna Posted September 9, 2011 Share Posted September 9, 2011 hi every1 im driveing crazy with this... i have a database with a field with descriptions of products named "DESCRIP" in the main page i have a text field where user write the words to search for i wanrt make an "SELECT * FROM models where descrip like xxx OR descrip like xxx... bla bla bla" i wrote this code but send me an error... waht can be wrong??? $word='"SELECT * FROM models WHERE descrip LIKE ' ."'"; $srched=$_POST['words']; $pieces = explode(" ", $srched); $all=count($pieces)-1; for($i=0;$i<=$all;$i ++) { $word = $pieces[$i] ."'"; if($i!=$all) { $word .= $word . ' OR descrip like ' . "'"; } else { $word .= $word .'"' . ";"; } } $colname=$word; mysql_select_db($database_zero1, $zero1); $query_srch = $colname; $buscar = mysql_query($query_srch, $zero1) or die(mysql_error()); $row_srch = mysql_fetch_assoc($srch); $totalRows_srch = mysql_num_rows($srch); tnx in advcd Link to comment https://forums.phpfreaks.com/topic/246804-filter-database-from-words-in-an-array/ Share on other sites More sharing options...
voip03 Posted September 9, 2011 Share Posted September 9, 2011 Please read it and if you like please re arranges the logic. http://www.tizag.com/mysqlTutorial/mysqlwhere.php Link to comment https://forums.phpfreaks.com/topic/246804-filter-database-from-words-in-an-array/#findComment-1267459 Share on other sites More sharing options...
Pikachu2000 Posted September 10, 2011 Share Posted September 10, 2011 I believe this is what you're talking about, but from the description it sounds like you may be better off with a FULLTEXT search. <?php // this code assumes you've already properly escaped the string data . . . $words = ' some random search text'; // lots of extra spaces $words = preg_replace('~\s{2,}~', ' ', trim($words)); // get rid of extra spaces $words = explode(' ', $words); explode the string on the spaces $words = implode("%' OR field LIKE '%", $words); // implode the array with the separator shown $query = "SELECT field1, field2 FROM table WHERE field LIKE '%" . $words . "%'"; //build the query string Returns: SELECT field1, field2 FROM table WHERE field LIKE '%some%' OR field LIKE '%random%' OR field LIKE '%search%' OR field LIKE '%text%' Link to comment https://forums.phpfreaks.com/topic/246804-filter-database-from-words-in-an-array/#findComment-1267556 Share on other sites More sharing options...
Karyna Posted September 13, 2011 Author Share Posted September 13, 2011 TNX TO ALL but i finally get it the mistake was in the double quotes i this lines $word='"SELECT * FROM models WHERE descrip LIKE ' ."'"; |____ this double quote must go!!! $srched=$_POST['words']; $pieces = explode(" ", $srched); $all=count($pieces)-1; for($i=0;$i<=$all;$i ++) { $word = $pieces[$i] ."'"; if($i!=$all) { $word .= $word . ' OR descrip like ' . "'"; } else { $word .= $word .'"' . ";"; |__ this double quote mus t go to hell too!!! } } $colname=$word; mysql_select_db($database_zero1, $zero1); $query_srch = $colname; $buscar = mysql_query($query_srch, $zero1) or die(mysql_error()); $row_srch = mysql_fetch_assoc($srch); $totalRows_srch = mysql_num_rows($srch); Link to comment https://forums.phpfreaks.com/topic/246804-filter-database-from-words-in-an-array/#findComment-1268804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.