3 Replies - 259 Views - Last Post: 06 April 2013 - 07:18 PM Rate Topic: -----

#1 zohaib55  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 29-January 12

Annoying error

Posted 06 April 2013 - 12:36 PM

i having these two annoying errors please help me out how to fix these.

Warning: array_push() expects parameter 1 to be array, boolean given in D:\xampp\htdocs\simple_blog\inc\functions.inc.php on line 31

Warning: array_pop() expects parameter 1 to be array, boolean given in D:\xampp\htdocs\simple_blog\index.php on line 20

<?php 
include_once 'inc/functions.inc.php';
include_once 'inc/db.inc.php';

$db = new PDO(DB_INFO,DB_USER,DB_PASS);

$id = (isset($_GET['id'])) ? (int) $_GET['id']:NULL;

$e = retrieveEntries($db,$id);
$fulldisp =array_pop($e);

$e =sanitizeData($e);

if ($fulldisp ==1)
{
?>
  <h2> <?php echo $e['title']?></h2>
  <p><?php echo $e['entry']?></p>
  <p class="backlink">
  <a href="./">Back to Latest Entries</a>
  </p>	
<?php }


else 
{
	foreach($e as $entry)
	{		
?>
     <p>
        <a href ="?id=<?php echo $entry['id'];?>">
                     <?php echo $entry['title'];?>
        </a>
     </p>
	<?php }
}


?>

function retrieveEntries($db,$id=NULL)
{
	
	if(isset($id))
	{
		$sql ="SELECT title,entry FROM entries WHERE id =? LIMIT =1";
		$stmt=$db->prepare($sql);
		$stmt->execute(array($_GET['id']));
        $e=$stmt->fetch();
        
        $fulldisp=1;		
	}
	else
	{
		$sql="Select id,title From entries ORDER BY created DESC";
	    
		foreach ($db->query($sql) as $row)
		{
			$e[] =array($row['id'],$row['title']);
		}
		$fulldisp = 0;
		if(!is_array($e))
		{
			$fulldisp =1;
			$e = array('title'=>'No Entrie Yet',
					
					'entry'=>'<a href="/admin.php">Post an entry!</a>' 
					);
		}

	}
	array_push($e, $fulldisp);
	
	return  $e;
}

This post has been edited by JackOfAllTrades: 06 April 2013 - 03:15 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Annoying error

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5315
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: Annoying error

Posted 06 April 2013 - 12:47 PM

Please fix your code tags. Its not rocket science and its explained all over including the box where you pasted your code.

Posted Image

I don't do PHP and yet that error message seems really clear.

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

So it sounds like it is saying $e is a boolean and the function is expecting an array.

$fulldisp =array_pop($e);
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5960
  • View blog
  • Posts: 23,235
  • Joined: 23-August 08

Re: Annoying error

Posted 06 April 2013 - 03:16 PM

Another person trying to write PHP with SQL and just assumes that SQL queries never fail.
Was This Post Helpful? 0
  • +
  • -

#4 Sho Ke  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 107
  • View blog
  • Posts: 246
  • Joined: 13-October 11

Re: Annoying error

Posted 06 April 2013 - 07:18 PM

Error handling is a HUGE part of programming, especially when interacting with databases.
I'm guessing when you get theses erorrs, $id is set, meaning this line of code will execute:
 $e=$stmt->fetch();



fetch will reutrn false if it fails, which would explain why $e is a boolean(false) when you try to use it in an array_push().

Ah, it's nice to have editor functions working again!

EDIT: The manual is your friend! When a variable is getting a weird value, start backtracking in your code and look up any functions you're using to set its value. Look at the return types on success, and the return types on failure.

This post has been edited by Sho Ke: 06 April 2013 - 07:21 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1