Jump to content

mysql "OR"


ohdang888

Recommended Posts

i think i need to use a Mysql OR statement..... But i don't know how (and google isn;t helping either)

 

I am creating a search option within my site. It searches for many things, through tags, titles, etc. So i have been using different queries for different field..

 

This is what mine is right now.

1. query for tags Like search

2. query for title Like search

etc.

 

But that is creating many duplicates results...

 

Any ideas?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/98652-mysql-or/
Share on other sites

<?php
echo '<table class="main_body"><tr><td width="150">';		
// code For multiple tags
		$tags = explode(',', $search);

		//set within the varable names

		for($i=0; $i<count($tags); $i++){

	    $res[]=trim($tags[$i]);
	    }
	    
		foreach($res as $x){

	  
		   mysql_select_db("games") or die(mysql_error());
		  $result = mysql_query("SELECT `title`,`type`,`game_picture_url` FROM `game` WHERE upper(tags) LIKE upper('%$x%')  ")or die(mysql_error());  
		  while($row = mysql_fetch_assoc($result)){  

		  
				 echo '<center><table width ="150px"><tr><td>';	
			   echo '<a href="game.php?title=';
			   echo $row['title'].'">';
			   echo '<IMG SRC="gamepic/';
			   echo $row['game_picture_url'];
			   echo '"'.' WIDTH="90"'.' HEIGHT="90"></a></td>';	
			   echo '<td class="search_game_info"><a href ="game.php?title=';//start of link code
			   echo $row['title'];
			   echo '">';
			   echo $row['title'];
			   echo '</a></br>';// end of link code
			   echo '(';
			   echo $row['type'];
			   echo ')</td></tr></table>';
			   
			   
			   echo '</td></tr><tr><td>';
			}

	      
		}	




	$result = mysql_query("SELECT * FROM `game` WHERE upper(title) LIKE upper('%$search%') ")or die(mysql_error());
		while($row = mysql_fetch_assoc($result)){  
	   echo '<center><table width ="150px"><tr><td>';	
	   echo '<a href="game.php?title=';
	   echo $row['title'].'">';
	   echo '<IMG SRC="gamepic/';
	   echo $row['game_picture_url'];
	   echo '"'.' WIDTH="90"'.' HEIGHT="90"></a></td>';	
	   echo '<td class="search_game_info"><a href ="game.php?title=';//start of link code
	   echo $row['title'];
	   echo '">';
	   echo $row['title'];
	   echo '</a></br>';// end of link code
	   echo '(';
	   echo $row['type'];
	   echo ')</td></tr></table>';
	   
	   
	   echo '</td></tr><tr><td>';
	}

	echo '</td></tr></table>';
?>

 

Link to comment
https://forums.phpfreaks.com/topic/98652-mysql-or/#findComment-504872
Share on other sites

try

foreach($res as $x) $w[] = " upper(tags) LIKE upper('%$x%') ";
$w = implode('OR', $w);		
mysql_select_db("games") or die(mysql_error());
$result = mysql_query("SELECT `title`,`type`,`game_picture_url` FROM `game` WHERE $w")or die(mysql_error());  
while($row = mysql_fetch_assoc($result)){  
echo '<center><table width ="150px"><tr><td>';	
echo '<a href="game.php?title=';
echo $row['title'].'">';
echo '<IMG SRC="gamepic/';
echo $row['game_picture_url'];
echo '"'.' WIDTH="90"'.' HEIGHT="90"></a></td>';	
echo '<td class="search_game_info"><a href ="game.php?title=';//start of link code
echo $row['title'];
echo '">';
echo $row['title'];
echo '</a></br>';// end of link code
echo '(';
echo $row['type'];
echo ')</td></tr></table>';
echo '</td></tr><tr><td>';
}

Link to comment
https://forums.phpfreaks.com/topic/98652-mysql-or/#findComment-504905
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.