PDO One Time Connection?

by exma
7 replies
Hi there,

Is this real that I can connect one time to mysql using PDO? I mean is that right to do that?
If I use the autoloader and I call every time the DATABASE class I'm connecting every time when I call to database class?
#connection #pdo #time
  • Profile picture of the author David Beroff
    Yes, PDO is the layer you should be using:
    PHP: PDO - Manual
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[10164622].message }}
    • Profile picture of the author exma
      Originally Posted by David Beroff View Post

      Yes, PDO is the layer you should be using:
      PHP: PDO - Manual
      What do you mean "PDO is the layer"?
      {{ DiscussionBoard.errors[10164633].message }}
      • Profile picture of the author David Beroff
        Originally Posted by exma View Post

        What do you mean "PDO is the layer"?
        PHP offers several database abstraction layers, of which, PDO is generally recommended.
        Signature
        Put MY voice on YOUR video: AwesomeAmericanAudio.com
        {{ DiscussionBoard.errors[10164909].message }}
        • Profile picture of the author exma
          Originally Posted by David Beroff View Post

          PHP offers several database abstraction layers, of which, PDO is generally recommended.
          Yeah I know, but the question if I can connect one time to DB and if its right to do that?
          The second question: if I make DATABASE class using PDO and I call everytime to this class so I reconnect everytime to DB?
          {{ DiscussionBoard.errors[10165235].message }}
          • Profile picture of the author David Beroff
            Originally Posted by exma View Post

            Yeah I know, but the question if I can connect one time to DB and if its right to do that?
            Yeah; why not? No one cares if you connect once or a thousand times.

            Originally Posted by exma View Post

            The second question: if I make DATABASE class using PDO and I call everytime to this class so I reconnect everytime to DB?
            I'm not sure why you need to create another layer around PDO. Just call it directly; we're talking maybe two or three lines of code.

            In my own software, I connect to the database once at the beginning of the PHP invocation, then I pass the open database handle to any function that needs it, and finally close it ("$db = NULL;") at the very end.
            Signature
            Put MY voice on YOUR video: AwesomeAmericanAudio.com
            {{ DiscussionBoard.errors[10165942].message }}
            • Profile picture of the author exma
              Originally Posted by David Beroff View Post

              Yeah; why not? No one cares if you connect once or a thousand times.
              Someone says me if I connect one time I'm calling one time the PDO class and run it only one time all processes. Then I just use the is set process.

              Originally Posted by David Beroff View Post

              I'm not sure why you need to create another layer around PDO. Just call it directly; we're talking maybe two or three lines of code.

              In my own software, I connect to the database once at the beginning of the PHP invocation, then I pass the open database handle to any function that needs it, and finally close it (" = NULL;") at the very end.
              I do that because I build all system on OOP and I'm use the autoloader to call classes and methods. Then I can use the specific method like a method to get a row. What I do its use only one line of code to run a query and all filtering on the query. Its to save a processes on server and code lines. I'm writing only one time all method to get a row and then I use every time only one row like:

              PHP Code:
              Database::getrow("SELECT * FROM database WHERE users = ?", ['usernames_array']); 
              {{ DiscussionBoard.errors[10168315].message }}
  • Profile picture of the author David Beroff
    OK. So you're good to go.
    Signature
    Put MY voice on YOUR video: AwesomeAmericanAudio.com
    {{ DiscussionBoard.errors[10168565].message }}

Trending Topics