Page 1 of 1

Broken Explode?

Posted: Sun Dec 11, 2011 2:01 pm
by Baz
I have the following code from the database search tutorial and I seem to be getting empty arrays from it - Yet I can't see why!?
Any help would be great!
<?php 
function search_posts($term){
echo "keywords entered was $term<br/>";
	
	$keywords=explode(' ', mysql_real_escape_string($term));
	print_r($keywords);
	
}



?>

<form action="test.php" method="get">
          <input type="text" name="term"  /><br />
          <input type="submit" value="Search" />
        </form>
<?php 
  if(empty($_GET['term']) === false){
	search_posts($_GET['term']);
}
?>

Re: Broken Explode?

Posted: Sun Dec 11, 2011 2:10 pm
by jacek
Does the line
echo "keywords entered was $term<br/>";
Output what you expect ?

Also, you should really organise this code better, the function should be in a separate file and the processing code for the form should be at the top.

Re: Broken Explode?

Posted: Sun Dec 11, 2011 2:25 pm
by Baz
well in the example video it splits the input placing them in an array starting at 0 and lists what it has placed where. My example only ever has 0 in its array and does not list what it is that it has found.

Seems the $term variable is found but some how it is getting lost in the explode part.

Any ideas what I have done wrong?

Re: Broken Explode?

Posted: Sun Dec 11, 2011 4:43 pm
by jacek
Is that the entire code, the only thing I can think of is that you are not connected to the database which would make mysql_real_escape_string return false and not the escaped string.

Re: Broken Explode?

Posted: Sun Dec 11, 2011 5:11 pm
by Baz
Thank you for that it seems to of done the trick! 8-)

Hmmmmz odd indeed :roll: