Jump to content

Custom Class - Query within function not working....please help....


mtlhd

Recommended Posts

Going nutz here!  NOt sure what I did wrong in this snippet of code, but it fails to query.  I am only a year into being a web dev and OOP still fairly new to me.

 

This code works up until it tries to run 'mysql_query($query)', it echoes "Query failed. Check your SQL statement."

 

Where did I mess it up at?  Thanks PHP brains!

 

Class StripHTML {

     
//Select description column and strip it of HTML  (calls all functions)

function fixDESC(){

	$link = mysql_connect(***left out for obvious reasons!***);
     
	/* check connection */
	if (!$link) {
	   	 printf("Connect failed: %s\n", mysql_error());
	    exit();
	}

	$query = "SELECT item FROM table;";

	if ($result = mysql_query($query)){ 

		$count = 0; 

        /* fetch associative array */
		while ($row = mysql_fetch_assoc($result)) {

		$desc = $this->strip_html_tags($row);
		$update = "UPDATE table SET item = '" . $desc . "' WHERE product_desc = '" . $row . "';";
    		
		mysql_query($update);

		$count++;
		}

		//* determine number of rows result set */
		printf("<br /><br /><b>Result set has fixed %d rows.\n</b>", $count);

	} else {
		echo "<span style='color: red;'><b>Query failed. Check your SQL statement.</b></span><br />";
	}

  
    if (!mysql_close($link)){
	    printf("Disconnect failed: %s\n", mysql_error());
    }
  
}


//REMOVE HTML Function
	  
          /**
          * Remove HTML tags, including invisible text such as style and
          * script code, and embedded objects.  Add line breaks around
          * block-level tags to prevent word joining after tag removal.
          */
          function strip_html_tags($var){
          
             preg_replace(
                 array(
                   // Remove invisible content
                     '@<head[^>]*?>.*?</head>@siu',
                     '@<style[^>]*?>.*?</style>@siu',
                     '@<script[^>]*?.*?</script>@siu',
                     '@<object[^>]*?.*?</object>@siu',
                     '@<embed[^>]*?.*?</embed>@siu',
                     '@<applet[^>]*?.*?</applet>@siu',
                     '@<noframes[^>]*?.*?</noframes>@siu',
                     '@<noscript[^>]*?.*?</noscript>@siu',
                     '@<noembed[^>]*?.*?</noembed>@siu',
                   // Add line breaks before and after blocks
                     '@</?((address)|(blockquote)|(center)|(del))@iu',
                     '@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
                     '@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
                     '@</?((table)|(th)|(td)|(caption))@iu',
                     '@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
                     '@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
                     '@</?((frameset)|(frame)|(iframe))@iu',
                 ),
                 array(
                     ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
                     "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
                     "\n\$0", "\n\$0",
                 ),
                 $var);
                 
             return strip_tags($var);
            
     		}
     		

//end class
}

 

 

I think it would be better to output the actual error. I think the issue is the ; in the SQL but not sure. I seem to remember issues with that.

 

Change this:

 

} else {
         echo "<span style='color: red;'><b>Query failed. Check your SQL statement.</b></span><br />";
      }

 

to

} else {
         echo "<span style='color: red;'><b>Query failed. Check your SQL statement.<br />" . $query . " <br />MySQL Error: " . mysql_error() . "</b></span><br />";
      }

 

At least in development that will help you immensely to find the issue.

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.