Click to See Complete Forum and Search --> : Wildcard in SQL Query (WHERE clause)


96turnerri
11-20-2004, 06:34 PM
i know using a wildcard is not possible in an sql query (well under where caluse anyway) eg

$sql = "SELECT team1, team2, id FROM pools WHERE * = '$user'"; will not work, however i need to do an sql query to search basically 100 colomns, i dont want to do this
$sql = "SELECT team1, team2, id FROM pools WHERE 01 = '$user' OR 02 = '$user' .... '99' = '$user'";

anyone have any better suggestions, i can think of a few work arounds but what would you say is easier

Thanks
Rich

ShrineDesigns
11-20-2004, 06:52 PM
dynamically create the where clause with php

example:<?php
$link = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $link);

$fields = mysql_list_fields('database', 'pools', $link);
$where = "WHERE ";
$sep = "";

foreach($fields as $field)
{
$where .= $sep . "`$field` = '{$user}'";
$sep = " OR ";
}
$result = mysql_query("SELECT `team1`, `team2`, `id` FROM `pools` $where", $link);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// ...
}
?>

96turnerri
11-20-2004, 06:58 PM
hmm intreseting suggestion shrine, i will give it a go see what proccess time is like on that

ShrineDesigns
11-20-2004, 07:16 PM
you might check the MySQL manual for a different approach