Solved

PDO fetch returns the sql query

Posted on 2014-03-08
10
294 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
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 39915212
OH BOY
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 108

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to dynamically set the form action using jQuery.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now