SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Question on using PDO to access a MySQL Database...

    Sorry if this is an easy or (possibly dumb) question, but I am a complete newbie to PHP and somewhat to OOP as well...

    Using PDO, do i need to create a new PDO Object for each query? Or can I create one object that can then be passed to other classes and so on?

    For example, I have the following line of code to create the object:

    $db = new PDO($dsn, $user, $pass);

    So now I have an instance of the PDO object stored as $db. Can that object now be used to run multiple statements? For instance if I have another object that needs database access so say the first attribute it requires is a PDO object, can I just pass this object on and then each new object instance just runs in own statement?

    For example:

    $user = new User($db, $userID);
    $calendar = new Calendar($db, $userID);

    Like I said, sorry if this seems obvious. I think I have the correct understanding, but I figured it is better safe that sorry. Thanks for everyones help!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by josheby
    Using PDO, do i need to create a new PDO Object for each query?
    Not unless you need to open another connection.

    Quote Originally Posted by josheby
    can I create one object that can then be passed to other classes and so on?
    Yes, so long your referring to the same database.

  3. #3
    SitePoint Member
    Join Date
    May 2007
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great. That was my understanding. Thanks for the quick response.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •