Jump to content

filter database from words in an array


Karyna

Recommended Posts

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

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%'

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);

 

 

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.