Tag Archives: foreach

php PDO fetchAll() – while not working, foreach works

Questions: I would like to know if i’m doing fine OR fetchAll() doesn’t work with WHILE. here is an exemple $db=new PDO(“mysql:host=” .$dbhost. “; dbname=” . $dbname, $dbuser, $dbpass); $page=$db->prepare(“SELECT * FROM page”); $page->execute(); foreach ($page->fetchAll(PDO::FETCH_ASSOC) as $row) { //echo a row //is working } however, i if try looping with a while while ($row=$page->fetchAll(PDO::FETCH_ASSOC)){… Read More »

Optimization of foreach for thousands items

Questions: I’m running the code below over a set of 25,000 results. I need to optimize it because i’m hitting the memory limit. $oldproducts = Oldproduct::model()->findAll(); /*(here i have 25,000 results)*/ foreach($oldproducts as $oldproduct) : $criteria = new CDbCriteria; $criteria->compare(‘`someid`’, $oldproduct->someid); $finds = Newproduct::model()->findAll($criteria); if (empty($finds)) { $new = new Newproduct; $new->someid = $oldproduct->someid; $new->save();… Read More »

PHP PDO with foreach and fetch

Questions: The following codes: <?php try { $dbh = new PDO(“mysql:host=$hostname;dbname=$dbname”, $username, $password); echo “Connection is successful!<br/>”; $sql = “SELECT * FROM users”; $users = $dbh->query($sql); foreach ($users as $row) { print $row[“name”] . “-” . $row[“sex”] .”<br/>”; } foreach ($users as $row) { print $row[“name”] . “-” . $row[“sex”] .”<br/>”; } $dbh = null;… Read More »

PHP simple foreach loop with HTML [closed]

Questions: I am wondering if it will work best to actually write the following for example: <table> <?php foreach($array as $key=>$value){ ?> <tr> <td><?php echo $key; ?></td> </tr> <?php } ?> </table> So basically embedding HTML inside foreach loop but without using echo to print the table tags. Will this work? I know in JSP… Read More »