Solved

PHP PDO Noob Question

Posted on 2012-04-12
3
326 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 83

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 83

Accepted Solution

by:
Dave Baldwin earned 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WPF - Tooltips for ComboBox items 5 107
Powershell File Sort 8 45
AWK: Am I duplicating the comma delimiter in this code? 5 30
Permission on folders 6 20
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

685 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