0

I have a large table and would like to search all of the fields in one go but some of the fields are dates so the search I have created falls over when it hits those fields.

Is there a way to exclude certain types of fields when using this type of search?

$table = 'accounts';
$condition = 'tracey';

$sql = "SHOW COLUMNS FROM accounts";

$result = mysqli_query($mysqli,$sql);

if (mysqli_num_rows($result) > 0)
{
    $i=0;
    while ($row = mysqli_fetch_array($result))
    {
        if($i==0)
        {
            $q="select * from ".$table." where ".$row[0]." LIKE %".$condition."%";
        }
        else
        {
            $q.=" or ".$row[0]."="." LIKE %".$condition."%";
        }
        $i++;
    }
}

$sql = $q;
$result = mysqli_query($mysqli,$sql) or die(mysqli_error($mysqli));
$row = mysqli_fetch_array($result);
$answer = $row['phone_office'];
echo '<br><br>answer = '.$answer;

Or perhaps someone can suggest a better way of searching all of the fields in a table in one go?

Kevin
  • 41,694
  • 12
  • 53
  • 70
tatty27
  • 1,553
  • 4
  • 34
  • 73

1 Answers1

0

To exclude certain types of fields You need to change the query SHOW COLUMNS FROM accounts with this one:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS 
WHERE (table_name=".$table.")AND(NOT(DATA_TYPE IN ('field_type_1','field_type_2',...)));

Where field_type_i is the name of an excluded type (for example 'timestamp')

tobias_k
  • 81,265
  • 12
  • 120
  • 179