Page 1 of 1

search database help

Posted: Mon Oct 03, 2005 10:32 pm
by elecktricity
okay I was wondering when your searching a database you used this line:

Code: Select all

$query = sprintf("SELECT * FROM content WHERE coloum like '%".$searchterm."%'";
but what if I wanted to search multiple coloums at a time?

EDIT: i've also tried this:

Code: Select all

$query = sprintf("SELECT * FROM content WHERE id LIKE '%searchterm%' OR game LIKE '%searchterm%' OR console LIKE '%searchterm%' OR author LIKE '%searchterm%' OR site LIKE '%searchterm%' OR small LIKE '%searchterm%' OR full LIKE '%searchterm%'";

Posted: Tue Oct 04, 2005 12:29 am
by alvinphp
This might be what you want. You can concatenate the columns you want to search.

Code: Select all

$query = sprintf("SELECT * FROM content WHERE coloum_a + colum_b + colum_c like '%".$searchterm."%'";

Posted: Tue Oct 04, 2005 11:47 am
by elecktricity
not sure why but it dosnt seem to like that script, am I doing something wrong..?

/me makes sure I didnt miss a semi colon

Code: Select all

<?php
$dbh=mysql_connect ("localhost", "user", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("sitenamedb"); 
$query = sprintf("SELECT * FROM content WHERE game + console + author + small + full like '%".$searchterm."%'",
   mysql_real_escape_string(searchterm));

$result = mysql_query($query);

if (!$result) {
   $message  = 'Invalid query: ' . mysql_error() . "\n";
   $message .= 'Whole query: ' . $query;
   die($message);
}

while ($row = mysql_fetch_assoc($result)) {

   echo '<table width=\'455\' cellpadding=\'0\' cellspacing=\'0\' border=\'0\' class=\'header1\'>';
   echo '<tr>';
   echo '<th colspan=\'2\' height=\'20\' class=\'header2\'>';
   echo $row['game'];
   echo '</th>';
   echo '</tr>';
   echo '<tr>';
   echo '<td valign=\'middle\' width=\'90\' height=\'70\' class=\'header3\'><img src=\'http://site.com/uploads/';
   echo $row['pt1'];
   echo '\' border=\'0\' alt=\'\'></td>';
   echo '<td valign=\'top\' class=\'header4\'>';
   echo $row['small'];
   echo '</td>';
   echo '</tr>';
   echo '</table>';

}
?>
it's giving back this error
Warning: sprintf(): Too few arguments in /home/rootbee/public_html/vividgamers/search.php on line 5
Invalid query: Query was empty Whole query:

Posted: Tue Oct 04, 2005 7:15 pm
by John Cartwright

Code: Select all

$query = sprintf("SELECT * FROM content WHERE game + console + author + small + full like '%%s%'",
   mysql_real_escape_string($searchterm));
that should work. I suggest you read sprintf

Posted: Tue Oct 04, 2005 7:30 pm
by elecktricity
Thanks for the link, i've been trying to figure this one out for a while. Ill go do some more reading on this one.