PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 11th, 2013, 02:59 PM
ud2006 ud2006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 230 ud2006 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 20 h 39 m
Reputation Power: 7
PDO Fetch users from database returns only one user

I try to fetch users from a mysql database with PDO, therefor I created a class.
I select certain columns from the database and I know there are 2 records in that database, but only the second user is displayed.

It's being displayed inside an html table (through php code) and I believe that code is correct (if not please correct me), so I have no clue of what the problem is.

Here is the code I use (I originally have it in a try try catch block):
PHP Code:
 $this->pdo $this->connectMySql();
$query 'SELECT id, username, account_type FROM users';
$stmt $this->pdo->prepare($query);
//print_r($stmt);
if(!$stmt->execute()){
    return 
false;
}
$nart $stmt->rowCount();
if (
$nart == 0) {
    return 
"Geen gebruiker gevonden";
}
while (
$row $stmt->fetch(PDO::FETCH_ASSOC)) {
    
print_r($row);
    
$userResult "<tr>";
    
$userResult .= "<td>" $row['id'] . "</td>";
    
$userResult .= "<td>" $row['username'] . "</td>";
    
$userResult .= "<td><img src=\"images/yes.png\" width=\"24px\"/></td>";
    
$userResult .= "<td>" $row['account_type'] . "</td>";
    
$userResult .= "<td><a href=\"edit_user.php?user=" $row['id'] . "\"><img src=\"images/edit.png\" width=\"24px\"/></a></td>";
    
$userResult .= "<td><a href=\"delete_user.php?user=" $row['id'] . "\" onclick=\"return confirm('Weet je deze pagina wilt verwijderen? Dit kan niet ongedaan gemaakt worden!');\"><img src=\"images/trash.png\" width=\"24px\"/></a></td>";
    
$userResult .= "</tr>";
}
$this->pdo null;
return 
$userResult


I even tried to change the query to *, but even this displays only 1 user.
I believe the <tr></tr> is set right, because each row of the table contains a user.
I even use a print_r($row) on the fetch which displays both users but not in the table. When I change the fetch to fetchAll it just extents the array.

This is the array I get:
PHP Code:
Array ( [id] => [username] => Super-Admin [account_type] => Super-admin ) Array ( [id] => [username] => Testing [account_type] => Beheerder 


Just for further info, the user that does get displayed is the second record in the database (the username: Testing), the first record is not displayed (username:Super-Admin).

If someone notices a typo, please let me know, cause I lost it.
Thanks in advanced.

Last edited by ud2006 : February 11th, 2013 at 03:04 PM. Reason: add some more info

Reply With Quote
  #2  
Old February 11th, 2013, 03:21 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Check your HTML source to see if the data is in there with a bad tag or something.

You can't print_r inside a table, it makes invalid HTML. Maybe that's your issue.

It looks like both records are being fetched and returned, but the HTML just isn't working.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old February 11th, 2013, 03:25 PM
ud2006 ud2006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 230 ud2006 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 20 h 39 m
Reputation Power: 7
Thanks for the reply, I've checked the html, but nothing wrong found. Just the record inserted into the table. As for the print_r, I have removed it, but still the same result.

Reply With Quote
  #4  
Old February 11th, 2013, 03:51 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
So you're saying that this code produced 2 print_r outputs, but only 1 table row? That's not even really possible.

Reply With Quote
  #5  
Old February 11th, 2013, 03:56 PM
ud2006 ud2006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 230 ud2006 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 20 h 39 m
Reputation Power: 7
Quote:
Originally Posted by ManiacDan
So you're saying that this code produced 2 print_r outputs, but only 1 table row? That's not even really possible.


No that's not what I am saying, the print_r is just for me a test to see what the fetch actually gets, and displays that array on the page. I just removed it to be sure that that doesn't stands in the way. The while loop only gives me at this time 1 table row of user back, which seems very strange because all the users are fetched.

The $userResult builds up inside the while loop to retrieve that database items.

The return $userResult; than will return the value to page, where I call the function I need (to show all users).

Reply With Quote
  #6  
Old February 11th, 2013, 03:58 PM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Ooooooh, I see.

Look, the first line:
PHP Code:
 $userResult "<tr>"
That resets $userResult. You have to use .= on every line.
PHP Code:
 $userResult ''
while (
$row $stmt->fetch(PDO::FETCH_ASSOC)) { 
    
print_r($row); 
    
$userResult .= "<tr>"

Reply With Quote
  #7  
Old February 11th, 2013, 04:01 PM
ud2006 ud2006 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 230 ud2006 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 20 h 39 m
Reputation Power: 7
Quote:
Originally Posted by ManiacDan
Ooooooh, I see.

Look, the first line:
PHP Code:
 $userResult "<tr>"
That resets $userResult. You have to use .= on every line.
PHP Code:
 $userResult ''
while (
$row $stmt->fetch(PDO::FETCH_ASSOC)) { 
    
print_r($row); 
    
$userResult .= "<tr>"

That did the trick, please explain, why did I need the extra line above the while loop? I was almost breaking my keyboard because of this.

Reply With Quote
  #8  
Old February 11th, 2013, 06:04 PM
BarryG BarryG is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2011
Location: Sydney Australia
Posts: 131 BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level)BarryG User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 7 h 51 m 28 sec
Reputation Power: 83
Quote:
Originally Posted by ud2006
why did I need the extra line above the while loop?


The first line inside the loop
PHP Code:
 $userResult .= "<tr>"

is shorthand for
PHP Code:
 $userResult $userResult "<tr>"

Without the $userResult = ''; before the loop, the first line inside the loop will find that $userResult doesn't exist.
But the first line in the loop must be a concat for the second and subsequent iterations of the loop.

Reply With Quote
  #9  
Old February 11th, 2013, 08:23 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,845 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 8 h 48 m 7 sec
Reputation Power: 813
Hi,

by the way, there are several things you can improve in your database code: When you don't have any parameters in your query, there's no need for a prepared statement. Just use query. And this low level "while ($row = fetch ...)" pattern known from the old MySQL extension is no longer necessary in PDO. A PDOStatement is iterable, so you can use it directly in a foreach loop:
PHP Code:
 $users_stmt $this->pdo->query('
    SELECT
        id
        , username
        , account_type
    FROM
        users'
);
foreach (
$users_stmt as $user) {
    
#...



Don't use fetchAll unless you actually physically need the rows in an array. Otherwise, it's just a waste of resources.

By the way, do you open and close the database connection for every single query? That makes no sense and creates a lot of unnecessary overhead. Just open the connection once in the script and reuse it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PDO Fetch users from database returns only one user

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap