Return to Snippet

Revision: 35198
at November 4, 2010 01:50 by JonnySnip3r


Initial Code
public function search($string)
	{
		if(strlen($string) > 40)
		{
			return "Woow too many words, please shorten your search.";
		}
		if(empty($string))
		{
			return "I'm a search, you type things in and I find. Please enter something and try again!";
		}
		if(strlen($string) <= 3)
		{
			return "Come on dude, you expect me to find summin with that? Type some more tags in!";
		}
		
		$x=0;
		// Teh string could be multiple searches so explode::
		$string = explode(" ", $string);
		
		foreach($string as $search)
		{
			$x++;
			if($x == 1)
			{
				@$sql .= "(blog_tags LIKE '%$search%')";
			}
			else
			{
				@$sql .= " OR (blog_tags LIKE '%$search%')";
			}
		}
		
		$sql = "SELECT blog_tags FROM subarc_blog WHERE $sql LIMIT 40";
		
		// TODO:: Count how many search results found::
		$stmt = $this->conn->prepare($sql);
		$stmt->execute();
		
		$meta = $stmt->result_metadata();
		while($field = $meta->fetch_field())
		{
			$var = $field->name;
			$$var = null;
			$params[$var] = &$var;
		}
		
		call_user_func_array(array($stmt,'bind_result'),$params);
		
		while($stmt->fetch())
		{
			return $params;
		}
		$stmt->close();
	}

Initial URL

                                

Initial Description
Im pretty new to oop php i have tried to create a php search function but im sure this needs alot of improvement. So add on :D

Initial Title
php Prepared Statement Search (Needs Improvement)

Initial Tags
php, search, function

Initial Language
PHP