Jump to content

So confused with PDO while loop


systemsgotyou

Recommended Posts

Hello. I am new. I have simple two step process I want to be done. When it does work the webpage keeps going (im guessing it's an endless loop)... I can't seem to get it to work: I know there's something I'm missing about this PDO array stuff.

<?php

include("database.php");
$time_ago = strtotime("-1 minute");


/*find subscribers over a month old with the thirtydaysubs table*/

$checker = "SELECT emailaddress FROM aa_thirtydaysubs WHERE subscribedate < $time_ago";
$sthandler = $database->prepare($checker);
$sthandler->execute();


/*then move it to main list and remove from 30 day list*/

$data = array();
$data = $sthandler->fetch();


if ($sthandler->columnCount()){
while($sthandler->fetchAll(PDO::FETCH_ASSOC) !== 0){

$email2 = $data->fetchColumn(0);

$newlist= "2";
$updatelistid = "UPDATE zzemail_list_subscribers set listid = :newlist WHERE emailaddress = :email";
$sthandler3 = $database->prepare($updatelistid);
$sthandler3->execute(array(':email' => $email2, ':newlist' => $newlist ));



$clearars = "DELETE FROM aa_thirtydaysubs WHERE	emailaddress= :email";
$sthandler6 = $database->prepare($clearars);
$sthandler6->execute(array(':email' => $email2));
}
}
?>
Link to comment
https://forums.phpfreaks.com/topic/285232-so-confused-with-pdo-while-loop/
Share on other sites

fetchAll returns an array of results. You'll want to use a foreach loop instead

foreach($sthandler->fetchAll(PDO::FETCH_ASSOC) as $data){

$email2 = $data['emailaddress'];

$newlist= "2";
$updatelistid = "UPDATE zzemail_list_subscribers set listid = :newlist WHERE emailaddress = :email";
$sthandler3 = $database->prepare($updatelistid);
$sthandler3->execute(array(':email' => $email2, ':newlist' => $newlist ));



$clearars = "DELETE FROM aa_thirtydaysubs WHERE	emailaddress= :email";
$sthandler6 = $database->prepare($clearars);
$sthandler6->execute(array(':email' => $email2));
}

If you used the fetch method then you'd use a while loop.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.