Solved

PDO fetch returns the sql query

Posted on 2014-03-08
10
300 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
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 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 look for a specific file type in a local or remote server directory using PHP.

776 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