Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PDO fetch returns the sql query

Posted on 2014-03-08
10
Medium Priority
?
312 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:J N
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Author Comment

by:J N
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 1000 total points
ID: 39915199
while($row = $users->fetch(PDO::FETCH_ASSOC)){
echo "<pre>";
      print_r($row);
}
0
 
LVL 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 1000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Author Comment

by:J N
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 35

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:J N
ID: 39915279
Sorry guys i only saw one post when i was reading it so i awarded the points accordingly
0
 
LVL 111

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

609 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