Jump to content

Warning: array_push() expects parameter 1 to be array, boolean given


Aman Grover

Recommended Posts

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));

 

}

 

?>

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.