Solved

PDO fetch returns the sql query

Posted on 2014-03-08
10
297 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

919 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

20 Experts available now in Live!

Get 1:1 Help Now