Solved

PHP PDO Noob Question

Posted on 2012-04-12
3
328 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
[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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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ā€¦
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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).

688 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