a reusable query with modifications?

Posted by fusion on Stack Overflow See other posts from Stack Overflow or by fusion
Published on 2010-05-05T14:05:35Z Indexed on 2010/05/05 14:08 UTC
Read the original article Hit count: 204

Filed under:
|
|

i'm using this mysql query alongwith php to search for multiple keywords:

$query = "SELECT cQuotes, vAuthor, cArabic, vReference FROM ".$table." WHERE (";
  $countFields = count($arrayFields);
  while ($a < $countFields)
  {
    while ($b < $countSearch)
    {
      $query = $query."$arrayFields[$a] LIKE '%$arraySearch[$b]%'";
      $b++;
      if ($b < $countSearch)
      {
        $query = $query." AND ";
      }
    }
    $b = 0;
    $a++;
    if ($a < $countFields)
    {
      $query = $query.") OR (";
    }
  }
  $query = $query.")";
  $result = mysql_query($query, $conn)

i'd like to reuse this query with a few modifications to it (for instance, the WHERE clause remains the same, while i query the number of rows using COUNT), but it doesn't seem practical to repeat the code again for a few additions. any suggestions?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql