Aman Grover Posted May 4, 2012 Share Posted May 4, 2012 I am reading "PHP for absolute beginners" but I am stuck on page 183 where I get an error-- Warning: array_push() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\simple_blog\inc\function… on line 60 Warning: array_pop() expects parameter 1 to be array, boolean given in C:\xampp\htdocs\simple_blog\index.php on line 27 and Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\simple_blog\index.php on line 68 I don't know why it is returning $e as a boolean and not an array! I thought this is a great place to put up this question below is the code for functions.inc.php file--- <?php function retrieveEntries($db, $page, $url=NULL) { if(isset($url)) { $sql = "SELECT id, page, title, entry FROM entries WHERE url=? LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute(array($url)); $e = $stmt->fetch(); $fulldisp = 1; } else { $sql = "SELECT id, page, title, entry, url FROM entries WHERE page=? ORDER BY created DESC"; $stmt = $db->prepare($sql); $stmt->execute(array($page)); while($row = $stmt->fetch()) { $e[] = $row; $fulldisp = 0; } if(!is_array($e)) { $fulldisp = 1; $e = array( 'title' => 'No Entries Yet!', 'entry' => 'This Page Does Not Have An Entry Yet!!' ); } } array_push($e , $fulldisp ); return $e; } function sanitizeData($data) { if(!is_array($data)) { return strip_tags($data, "<a>"); } else { return array_map('sanitizeData', $data); } } function makeUrl($title) { $patterns = array( '/\s+/', '/(?!-)\W+/' ); $replacements = array('-' , ''); return preg_replace($patterns , $replacements , strtolower($title)); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/262053-warning-array_push-expects-parameter-1-to-be-array-boolean-given/ Share on other sites More sharing options...
MMDE Posted May 4, 2012 Share Posted May 4, 2012 First of all, just some proper formatting and use of the code tag to make it readable: <?php function retrieveEntries($db, $page, $url=NULL){ if(isset($url)){ $sql = "SELECT id, page, title, entry FROM entries WHERE url=? LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute(array($url)); $e = $stmt->fetch(); $fulldisp = 1; }else{ $sql = "SELECT id, page, title, entry, url FROM entries WHERE page=? ORDER BY created DESC"; $stmt = $db->prepare($sql); $stmt->execute(array($page)); while($row = $stmt->fetch()){ $e[] = $row; $fulldisp = 0; } if(!is_array($e)){ $fulldisp = 1; $e = array( 'title' => 'No Entries Yet!', 'entry' => 'This Page Does Not Have An Entry Yet!!' ); } } array_push($e , $fulldisp ); return $e; } function sanitizeData($data){ if(!is_array($data)){ return strip_tags($data, "<a>"); }else{ return array_map('sanitizeData', $data); } } function makeUrl($title){ $patterns = array( '/\s+/', '/(?!-)\W+/' ); $replacements = array('-' , ''); return preg_replace($patterns , $replacements , strtolower($title)); } ?> When I formatted it, I couldn't help but see that $e might never be set and $fulldisp always either are 1. I got no idea why you set $fulldisp = 0;, when this only run if $e becomes an array, and you later then check if $e is an array and then set $fulldisp to be 1. Makes very little sense. Think of this scenario where the sql query doesn't get any results. In both cases $e is certainly not made an array. Then you try to use array_push (pretty useless function) on the nothing being $e. Even if it had something, what do you expect array_push to do? I think $e[] = 1 would have done the same thing. At least then it would exist if it didn't already exist. Are you sure your sql queries do get any results? No errors? Now onto the next problem. You are not even giving us the entire code. I don't see you use array_pop anywhere. I'm going to go ahead and assume if you got error with array_push($e , $fulldisp );, then $e was not set and you use array_pop on the returned $e later on that wasn't set. Quote Link to comment https://forums.phpfreaks.com/topic/262053-warning-array_push-expects-parameter-1-to-be-array-boolean-given/#findComment-1342974 Share on other sites More sharing options...
soluicius Posted August 30, 2012 Share Posted August 30, 2012 MMDE thanks for your reply, I used your modified code and it works, but i get Notice: Undefined variable: e in C:\xampp\htdocs\simple_blog\inc\functions.inc.php on line 24 it's the same cod:) Quote Link to comment https://forums.phpfreaks.com/topic/262053-warning-array_push-expects-parameter-1-to-be-array-boolean-given/#findComment-1374057 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.