?
Solved

PHP PDO Noob Question

Posted on 2012-04-12
3
Medium Priority
?
353 Views
Last Modified: 2012-04-13
Can someone help me i think i miss something from OOP theory

The question is why why i have to assign $dbh->query($sql) into $STH in order to use $STH->fetch();

Moreover can you please guide on what to read in order to understand more?

Thanks

    $dbName = 'db';
    $dbConn = 'localhost';
    $dbUser = 'root';
    $dbPass = '';
    $dsn="mysql:host=$dbConn;dbname=$dbName;";
    try{
    $dbh= new PDO($dsn, $dbUser,  $dbPass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
       
    }
     catch (PDOException $e){
     
    echo 'error' .$e->getMessage();
    }
    $sql = "select question from tbl_choice ";
    //why i have to assign $dbh->query($sql) into $STH in order to use $STH->fetch();
    $STH = $dbh->query($sql);
       
    $STH->setFetchMode(PDO::FETCH_ASSOC);
     
    while ($row = $STH->fetch()) {
     
        echo $row['question'];
       
        }

Open in new window

0
Comment
Question by:Milton
  • 2
3 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 37841332
This is the PDO home page on PHP.net: http://us3.php.net/manual/en/book.pdo.php  In $STH = $dbh->query($sql); , $dbh is the PDO connection, $sql is the query, and $STH is your result set.  You need $STH to tell fetch() which result set to work with.  It's an identifier and there can be more than one result set on a page.
0
 
LVL 4

Author Comment

by:Milton
ID: 37841408
Yes i understood that $dbh is PDO connection and $sql is the query and i now i understand that $STH is an identifier so basically $STH becomes $dbh->query($sql)
but how  $STH can do $STH-> it becomes object?
or should i say how $dbh->query($sql) can do $dbh->query($sql)->fetch(); ?

i tried var_dump($STH) and it returned
object(PDOStatement)[2]
public 'queryString' => string ''select question from tbl_choice' (length=31)

Open in new window

0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 1500 total points
ID: 37841445
It's not just an identifier but also an object of a class that contains both the result that is returned and the methods to operate on them.  http://us3.php.net/manual/en/pdo.query.php  If you read the PDO pages I linked, you'll see that there is more than one way to get to the result set returned by the query.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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