zid Posted May 2, 2014 Share Posted May 2, 2014 Hi, Trying to get a query list posts that starts with A for example. straight up in the php file it works like a sharm as regular code. $string = 'A'; $stmt = $db->prepare("SELECT page_id, page_title FROM pages WHERE page_title LIKE :page_title"); $stmt->bindValue(':page_title', $string . '%', PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $row) { echo "<a href='index?page=". $row['page_id'] ."'>". $row['page_title'] ."</a>"; } but when trying to get this into a function, nothig is displayed. Please advice! What have I missed. // calling the function letterIndex('A'); // the function function letterIndex($string) { $stmt = $db->prepare("SELECT page_id, page_title FROM pages WHERE page_title LIKE :page_title"); $stmt->bindValue(':page_title', $string . '%', PDO::PARAM_STR); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $row) { echo "<a href='index?page=". $row['page_id'] ."'>". $row['page_title'] ."</a>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/288187-cannot-see-the-error-in-the-function/ Share on other sites More sharing options...
Solution requinix Posted May 2, 2014 Solution Share Posted May 2, 2014 (edited) $db is undefined. Because variables defined outside functions are not automatically defined inside functions. letterIndex($db, 'A'); function letterIndex($db, $string) {[edit] Find your php.ini and set error_reporting = -1 display_errors = onRestart your web server if you had to change that. PHP would have given you an error about calling a method on a non-object, cluing you into the problem with $db. Edited May 2, 2014 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/288187-cannot-see-the-error-in-the-function/#findComment-1477978 Share on other sites More sharing options...
zid Posted May 2, 2014 Author Share Posted May 2, 2014 Thanks requinix, that worked perfect! Quote Link to comment https://forums.phpfreaks.com/topic/288187-cannot-see-the-error-in-the-function/#findComment-1477979 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.