PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Dec 18, 2018 7:13 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Fri Nov 30, 2018 6:04 am 
Offline
Forum Contributor

Joined: Mon Jan 17, 2005 5:13 pm
Posts: 305
:roll: I'm trying to learn OOP and convert some old scripts to the latest. (Testing all this locally) I can't see why I cannot get either of the loops to work below. If I comment out var_export($row); everything gets out-putted so I know my connection and query is OK.
Any ideas as to what else I need to try as I've exhausted my research at the mo ?
Thank you.

Syntax: [ Download ] [ Hide ]
<?PHP $stmt = $pdo->prepare("SELECT * FROM queue ORDER BY customer_name ASC");
                        $stmt->execute();
                        $row = $stmt->fetchAll(PDO::FETCH_ASSOC);

                        if(!$row) exit('No rows');
        echo $stmt->rowCount();
                        //var_export($row);
                        //$stmt = null;

   while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
           
           echo $row['date_due'];
           echo $row['customer_name'];
        }
       
        $results = $stmt->fetchAll(PDO::FETCH_ASSOC);
        foreach($results as $row) {
               
                echo $row['date_due'];
            echo $row['customer_name'];
} ?>


Top
 Profile  
 
PostPosted: Fri Dec 14, 2018 11:16 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13585
Location: New York, NY, US
$stmt->fetchAll(PDO::FETCH_ASSOC); will fetch all the rows selected, so you are at the end of the dataset. You don't need to loop. Either use fetchAll() or loop and use fetch(),

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group