Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.