Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   PDO One Time Connection? (https://www.warriorforum.com/programming/1090845-pdo-one-time-connection.html)

exma 11th July 2015 11:36 AM

PDO One Time Connection?
 
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?

David Beroff 12th July 2015 11:54 AM

Re: PDO One Time Connection?
 
Yes, PDO is the layer you should be using:
PHP: PDO - Manual

exma 12th July 2015 12:06 PM

Re: PDO One Time Connection?
 
Quote:

Originally Posted by David Beroff (Post 10164622)
Yes, PDO is the layer you should be using:
PHP: PDO - Manual

What do you mean "PDO is the layer"?

David Beroff 12th July 2015 04:08 PM

Re: PDO One Time Connection?
 
Quote:

Originally Posted by exma (Post 10164633)
What do you mean "PDO is the layer"?

PHP offers several database abstraction layers, of which, PDO is generally recommended.

exma 12th July 2015 10:08 PM

Re: PDO One Time Connection?
 
Quote:

Originally Posted by David Beroff (Post 10164909)
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?

David Beroff 13th July 2015 07:47 AM

Re: PDO One Time Connection?
 
Quote:

Originally Posted by exma (Post 10165235)
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. :)

Quote:

Originally Posted by exma (Post 10165235)
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.

exma 14th July 2015 01:22 PM

Re: PDO One Time Connection?
 
Quote:

Originally Posted by David Beroff (Post 10165942)
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.

Quote:

Originally Posted by David Beroff (Post 10165942)
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']); 


David Beroff 14th July 2015 04:14 PM

Re: PDO One Time Connection?
 
OK. So you're good to go.


All times are GMT -6. The time now is 04:54 AM.