Solved

PHP PDO Noob Question

Posted on 2012-04-12
3
314 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 82

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 82

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now