Jump to content

Query to Exclude Certain Results


datoshway

Recommended Posts

I have a table with a comments column.  Some of those comments contain bad words.  How can I exclude the whole row if these certain words exist in the comments column?  Here is my query before the modification i'm talking about above.

$strQuery				= "SELECT * FROM Comments ORDER BY DATE_FORMAT( `dtAdded`, '%B%d,%Y' ) DESC LIMIT 3";
$queryGetComments			= db_query($strQuery);

 

 

Link to comment
https://forums.phpfreaks.com/topic/235750-query-to-exclude-certain-results/
Share on other sites

Ok here it is:

 

PHP

<?php
				if (db_num_rows($queryGetComments) > 0) {
					?>
        <?php
                                while ($objRow = db_fetch_object($queryGetComments)) {
								$intArticleID	= intval($objRow->intID);
                                    $intID	= stripslashes($objRow->intID);
                                    $Message	= stripslashes($objRow->Message);
                                    $Category	= stripslashes($objRow->Category);
								$dtAdded	= stripslashes($objRow->dtAdded);

                                     db_free_result($queryGetArticle);


							?> 
  <?php
                                    if (strlen($Message) > 252) {
                                        $Message	= substr($Message, 0, 252) . " ";
                                    }
                                    ?>
                                    
<?php echo $Message; ?>
...<a href='detail.php?id=<?php echo $intArticleID; ?>'>read full email</a>
     
  
<?php
                                }
                                
                                db_free_result($queryGetComments);
						?>                  
                        <?php
                } else {
                    ?>
<div id="errornoproducts">No emails at this time. Check back soon!</div>
<?php
                }
            ?>

 

 

Controller File

<?php

define("STR_RELATIVE_PATH", "");
define("STR_CURRENT_SECTION", "home");

require STR_RELATIVE_PATH . "_includes/_common_data.php";
require STR_RELATIVE_PATH . "_includes/_database.php";

db_connect();
db_select();

$Bad_Words = "crap";

	$strQuery	 = "select * from Comments where Message != $Bad_Words order by dtAdded desc";
	$queryGetComments = mysql_query($strQuery) or die(mysql_error());

?>

 

Thanks in advance!

 

 

OK I think I might have been wrong about that error I went back to my original code with your last suggestion and i'm getting this error.

 

 

Unknown column 'crap' in 'where clause'

 

With this being the query.

$Bad_Words = "crap";

	$strQuery	 = "select * from Comments where Message != $Bad_Words order by dtAdded desc";
	$queryGetComments = mysql_query($strQuery) or die(mysql_error());

 

okay i think that it might be because i forgot to put quotes around the variable...try this

$Bad_Words = "crap";

	$strQuery	 = "select * from Comments where Message != '$Bad_Words' order by dtAdded desc";
	$queryGetComments = mysql_query($strQuery) or die(mysql_error());

Ok got it, we are supposed to be used NOT LIKE instead of LIKE.  Your last suggestion was close.  So the query looks like this now and it works.

 

$Bad_Words = "crap";

	$strQuery = "select * from Comments where Message NOT LIKE '%$Bad_Words%' order by dtAdded desc";
	$queryGetComments = mysql_query($strQuery) or die(mysql_error());

 

Thanks for all your help.

 

Ok so I thought I had it but not so much.  It works with just one word (crap), if I add more then one word (crap,suck) then it doesn't work.  Any ideas??  Here is the full query..

$Bad_Words = "crap";

	$strQuery = "select * from Comments where Message NOT LIKE '%$Bad_Words%' order by dtAdded desc";
	$queryGetComments = mysql_query($strQuery) or die(mysql_error());

 

 

i found this code from a website...looks to be what you need...will need to change the query to NOT LIKE and the var values

$keywords= array("big","blue","sky");
$keycount= count($keywords);
for ($i=0; $i<$keycount; $i++)
{
$sql = "select * from ` domains ` where ` keyword ` like ' % " . $keywords [ $i ] . " % ' ";
$list = mysql_query($sql);
while ($lst = mysql_fetch_array($list))
{ ... }
mysql_free_result($list);
}

a quick/dirty solution:

 

don't do anything in your select... just get the recordset and pos-process it in php... in your case something like this:

 

  $badwords = array('/ bad1 /', 
                    '/ bad2 /', 
                    '/ bad3 /');  // this is the array of your bad-words ... could be a separate file for better maintenance

   $replace_with = ' ***** ';  // if you need to write something instead of the bad-words.

   $strQuery = "SELECT * FROM $table ORDER BY dtAdded DESC";  // replace $table with your table name
   $queryGetComments = mysql_query($strQuery) or die(mysql_error());

  // add any validation that you need

  while ($row = mysql_fetch_assoc($queryGetComments) {
      // anything you need here...
      echo preg_replace($badwords, $replace_with , $row['comment-column']);  // replace 'comment-column' with your comment column.. using echo only for testing purposes.
  }

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.