PHP- Typo on tutorial? -
i followed phpacademy tutorial on youtube , can't keywords print via echo. instead get:
`keywords` '%keyword%'
wth? code has been copied verbatim. platform problem?
is there issue on statement -> $where .= "
keywordslike '%keyword%'";
?
<?php include 'db.inc.php'; function search_results($keywords) { $returned_results=array(); $where=""; $keywords=preg_split('/[\s]+/', $keywords); $total_keywords = count($keywords); foreach($keywords $key=>$keyword){ $where .= "`keywords` '%keyword%'"; // where's issue? if($key != ($total_keywords-1)){ $where .= " , "; } } echo $where; } ?>
i did bit of refactoring on code, it's more clear. see comments in code.
include 'db.inc.php'; function search_results($keywords) { $where = array(); // skip empty results preg_split_no_empty flag $keywords = preg_split('/[\s]+/', $keywords, -1, preg_split_no_empty); foreach ($keywords $keyword) { // escape string (anti sql injection) $keyword = mysql_real_escape_string($keyword); // problem using keyword instead of $keyword $where[] = "`keywords` '%$keyword%'"; } echo implode(' , ', $where); }
Comments
Post a Comment