search database help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

search database help

Post 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%'";
alvinphp
Forum Contributor
Posts: 380
Joined: Wed Sep 21, 2005 11:47 am

Post 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."%'";
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post 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:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
elecktricity
Forum Contributor
Posts: 128
Joined: Sun Sep 25, 2005 8:57 pm
Location: Trapped in my own little world.
Contact:

Post 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.
Post Reply