Solved

PDO fetch returns the sql query

Posted on 2014-03-08
10
303 Views
Last Modified: 2014-03-09
Hi,

i have the following code
//GET THE USERS CURRENT ONLINE SESSION STATUS AND CHANGE IT IF NESSECARY
$sql ="SELECT count(*) FROM bb_users WHERE status='active' AND ";
$sql.="(online_status='online' OR online_status='away') AND uid!=1";
$query_users=$conn->query($sql);
$online_users = $query_users->fetch(PDO::FETCH_NUM);

Open in new window


and it works fine

now i have this code which is similar but only returns the sql query and i cannot understand why the above works while the below does not
$sql_users="SELECT ".$items_users." FROM bb_users WHERE status='active' AND (online_status='online' OR online_status='away') AND ";
$sql_users.="uid!=1 ORDER BY online_status_time ASC LIMIT ".$user_limit;
$users=$conn->query($sql_users);
while($users->fetch(PDO::FETCH_ASSOC)){
echo "<pre>";
	print_r($users);
}

Open in new window


i dont think i need to prepare a statement because i am only using it once and there are no user variables
0
Comment
Question by:M. Jayme Nagy
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39915178
the results from the second code are

PDOStatement Object
(
    [queryString] => SELECT uid, username, user_nickname, avatar, online_status, online_status_time FROM bb_users WHERE status='active' AND (online_status='online' OR online_status='away') AND uid!=1 ORDER BY online_status_time ASC LIMIT 8
)
0
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 39915199
while($row = $users->fetch(PDO::FETCH_ASSOC)){
echo "<pre>";
      print_r($row);
}
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 250 total points
ID: 39915208
$users is always the PDOStatement. You're not giving it another value, so why do you expect another value?
Try:
while($user=$users->fetch(PDO::FETCH_ASSOC)){
echo "<pre>";
	print_r($user);
}

Open in new window

HTH,
Dan
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39915212
OH BOY
0
 
LVL 58

Expert Comment

by:Gary
ID: 39915218
The first correct answer should be accepted not one which copies an already correct answer.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39915223
I agree the points should go to Gary, as he was faster.
But the author did ask for an explanation why his code was not working, not just a correction.
i cannot understand why the above works while the below does not
0
 
LVL 58

Expert Comment

by:Gary
ID: 39915226
Split is fine.
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39915279
Sorry guys i only saw one post when i was reading it so i awarded the points accordingly
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39915806
@jaymenagy: No points for this, please.  It's just an observation.

Going forward when you ask a question at EE, consider the value of waiting one day to close the question.   Not everyone is online paying attention all the time, but many of us check in once a day and if we find questions where the answer is missing or incomplete we may be able to add value.  If you ask a question and close it too quickly you will miss out on the benefit of "two heads are better than one."

Just a thought from someone who has been in the business a while and has seen a lot of programmers get into trouble because they forget the adage, "haste makes waste."
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 40
what do I need to host my own web sites? 13 50
check mysql insert 12 26
Use Select Query to Return Results as a Form 9 23
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question