mysql - PDO Update Multiple Rows -
mysql - PDO Update Multiple Rows -
bit of noob when comes pdo. have a table needs updated cron job daily. thought query table , each "active = 1" integer duration column convert days, "expiration" column , duration expiration , update expiration.
i not close getting work, trying basics now. homecoming duration in hours split 24 add together date column days , update each row appropriate new time.
the problem not know how update each row own information. have code below, updates each row new "expires" date ends beingness same though expires values different.
static function cronsetfeatured(){ try{ $db = new pdo('mysql:host=127.0.0.1;dbname=mydb', 'dbuser', 'pw' ); $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); }catch(pdoexception $e){ echo $e->getmessage(); die(); } $query = $db->query('select * featured_producers '); $now = date('y-m-d h:i:s'); while($r = $query->fetch(pdo::fetch_obj)){ $duration = $r->duration; $ddays = $duration / 24; $date = $r->date; $expires = date("y-m-d", strtotime($date. " + ".$ddays." days")); } $sql = "update `featured_producers` set `expires`= ? `id` != 0"; $q = $db->prepare($sql); $q->execute(array($expires)); echo $expires.'<br>'; }
well if want update
each record on own, query need part of loop body:
while($r = $query->fetch(pdo::fetch_obj)){ //calculate expiration $duration = $r->duration; $ddays = $duration / 24; $date = $r->date; $expires = date("y-m-d", strtotime($date. " + ".$ddays." days")); //get id (needed update) $id = $r->id; //updating $sql = "update `featured_producers` set `expires`= ? `id` != ?"; $q = $db->prepare($sql); $q->execute(array($expires, $id)); echo $expires.'<br>'; }
mysql pdo sql-update
Comments
Post a Comment