PHP OOP: Creating database class - php

I am making my first steps in the OOP world - please bear with me.
I know that having many ongoing mySQL connections open at the same time can be fatal for performance, so it should be a good idea to make a database class that takes this into account.
Is $this->session->write(...); going to result in a new mySQL connection being opened each time?
Or is that up to the "persistent connection" feature in mySQL?
Here's the code:
abstract class database {
function __construct() {
//mysql_connect()
}
}
class session extends database {
function write () {
// mysql_query(--.)
}
}

Is session handler some kind of specialized type of database? It is not, so don't use inheritance (a is a relationship). Session handler uses database so you should use composition (a has a relationship):
class Session {
protected $db;
public function __construct(DB $db) {
$this->db = $db;
}
public function write() {
$this->db->query(...);
}
}
$db = new DB(...);
$session = new Session($db);
Also, don't use mysql_*() functions. Use much more powerful PDO.
Returning to your question... mysql_connect() will be executed every time you create a new Database object. However in this case Session is an object of type Database so every time you write new Database() or new Session() the constructor is invoked, therefore mysql_connect() is invoked as well.
PS. mysql_connect() function won't create a new connection if specified connection already exists. Check 4th argument of this function to learn more.

mysql_query will create a new connection only if no previous mysql connection has been made. Otherwise, it will either use the connection you specify or the last connection opened with mysql_connect. http://php.net/manual/en/function.mysql-query.php

Why not
class database {
function __construct() {
//mysql_connect()
}
function write() {
//query the DB
}
}
I'm not sure of the syntax, I don't do OOP PHP. Anyway, in your structure above a new connection would be opened for each "session" instance so assuming you only create one instance of "session" you won't be openeing loads of database connections.

Related

pdo connection position in php class

I'm using PDO now and I have a number of classes.
Every time I use a class, I don't always use its database-related functions. Sometimes I keep using a class until at the end I might do some work with the database, like save this object to DB.
So I'm currently doing something like this:
class Something
{
protected $pdo;
function connect()
{
$this->pdo = new PDO( "mysql:host=".zConfig::read('hostname').";dbname=".zConfig::read('database'), zConfig::read('username'), zConfig::read('password'));
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
/* lots of functions doing lots of non-DB things */
function saveToDB()
{ $this->connect();
$this->pdo->prepare("Some SQL that saves some stuff");
// now do some DB-related pdo work
}
}
My question is - is this reasonable? Is this the way a lot of you code?
As I see it, there are 3 options:
The way I'm doing it - connecting to the database only in those
functions that are database related. But these means each time I run a DB-related function.
Open a connection in the constructor, in this case I only open it once, but also end up opening it even when I don't need it, so I feel like sometimes I'll be creating
connections for nothing.
On the other hand, I could use a singleton database class like this:
PDO Connections - max connections
I do have up to three related objects on a page, and sometimes I do and sometimes I don't need to connect to the DB more than once on a page. which design is safer?
You should use DI, in your class to instantiate a connection, when your object is defined ex:
class foo{
public $conn;
function __construct($conn){
$this->conn = $conn;
}
function doSomething(){
}
}
Now, if you find yourself, not wanting to instanciate a connection, whenever you
are on a page/work that does not need database connection, and significantly slows your page, while trying to connect, use the PDO attribute
ATT_PERSISTENT property like:
$conn = new PDO('mysql:host=localhost;dbname=xxx', 'xxx', 'xxx',
array( PDO::ATTR_PERSISTENT => true )
);
$object = new foo($conn);
Simply, once, you open a page, and connection is establish the ATTR_PERSISTENT method will store that connection, pretty much like sessions work, and will keep feeding your page, and helping you from creating a new connection to your db, every time you refresh a page, or go on to another page. Try, it.. and you'll see how faster your pages will load.
You can just use lazy way.
protected $pdo;
function connect()
{
if ($this->pdo === null)
{
$this->pdo = new PDO( "mysql:host=".zConfig::read('hostname').";dbname=".zConfig::read('database'), zConfig::read('username'), zConfig::read('password'));
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
}
Check this question.

PHP PDO class construction

I'm pretty new to both PDO and OOP. I'm trying to write a class that connects to a database and updates inserts and modifies it. I have several questions:
Is it good practices to connect to the database in the constructor?
Should the one class be updating, inserting, modifying and connecting or should it be split up into several classes?
Why is runQuery not working? I assume its because $pdo is defined in a different scope. How would I get this working?
If the class is include at the top of every page does that mean it will reconnect to the database every time a new page is loaded and will that cause security issues?
Apologies for the overload of questions. Thanks in advance for any answers.
<?php
class Login{
private $_username;
private $_password;
private $_host;
private $_database;
private $_driver;
//Connect to the database
function __construct($configFile){
$connectionDetails = parse_ini_file($configFile);
$this->_username = $connectionDetails['username'];
$this->_password = $connectionDetails['password'];
$this->_host = $connectionDetails['host'];
$this->_database = $connectionDetails['database'];
$this->_driver = $connectionDetails['driver'];
$pdo = new PDO("$this->_driver:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password);
}
public function loginAllowed($user, $pw){
$sth = $pdo->setFetchMode(PDO::FETCH_ASSOC);
print_r($sth);
}
public function runQuery($query, $params){
$sth = $this->pdo->prepare($query);
$sth->execute($params);
}
}
Because $pdo is a local variable in your constructor and your method loginAllowed. You should make it an instance variable (private $pdo) so you can call it through $this->pdo. I also suggest to use type hinting here, give the PDO class as a parameter in the constructor.
Example
<?php
class Login {
private $pdo;
// Your other instance variables
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
// Your other methods
}
$pdo = new PDO("...");
$login = new Login($pdo);
You shouldn't bother your class with reading settings and initialising your database connection (definitely read about separation of concerns), keep it out of your class. Just give the PDO object as a parameter (I used type hinting, that way you are forced to provide an object of the PDO type). Another advantage is that you can now make sure you have only one active database connection (you can manage this in your code base), creating multiple connections is unnecessary and definitely unwanted (performance wise).
Also use require_once to include your class definition. Otherwise you will get many errors about redeclaring (and you'd want to avoid that).
Connect to the db wherever you find it most convenient. Just try to make sure there's only ONE connection. More connections to the same db is a waste of time and resources.
The class you refer to is called a model in the MVC architecture. It usually does all the operations on a given table. I see nothing wrong in using a single class for all your needs - as long as the code is readable and maintainable.
It's not working because $pdo is a local variable. In the ctor, instantiate $this->pdo instead.
Including a class is not equivalent to instantiating it. A new instance will make another connection. Including it multiple times will only give you a multiple declaration error :). Use require_once instead. If you wish to use the instance in multiple files, I strongly suggest you do a quick search regarding the Singleton pattern. Using a singleton object will ensure you always have only one instance of your model object.
Don't bother with all the random stuff, just replacethis in your construct:
$pdo = new PDO("$this->_driver:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password);
with
$this->pdo = new PDO("$this->_driver:host=$this->_host;dbname=$this->_database", $this->_username, $this->_password);
and reference it as $this->pdo from now on. As simple as that!!
1) Is it good practices to connect to the database in the constructor?
No good.just connect befor query
if($this->pdo == null) {
$this->pdo = new PDO("....");
}
2) Should the one class be updating, inserting, modifying and connecting or should it be split up into several classes?
Add methods for class
3) Why is runQuery not working? I assume its because $pdo is defined in a different scope. How would I get this working?
use $this->pdo instead
4) If the class is include at the top of every page does that mean it will reconnect to the database every time a new page is loaded and will that cause security issues?
use static $pdo
then self::$pdo would be the only one connector
if(self::$pdo == null) {
self::$pdo = new PDO("....");
}

PHP transaction handling with multiple mysqli object

Lets put the case that we've got two separate classes, each owns a mysqli object to use for database operations.
class A{
protected $mysqliObject;
public function __constructor(){
$this->mysqliObject=new mysqli(...)
}
public function doSomething(){
$this->mysqliObject->query(...);
}
}
class B{
protected $mysqliObject;
public function __constructor(){
$this->mysqliObject=new mysqli(...)
}
public function doSomething(){
$this->mysqliObject->query(...);
}
}
The doSomething() methods use the $mysqliObject for a database query (SELECT, UPDATE, DELETE, etc...). I want to do the following outside:
// Start point
$aObject=new A();
$bObject=new B();
$aObject->doSomething();
$bObject->doSomething();
// End point
...but I want to roll back both queries if any of them fails. How can I do that?
Can I create a brand new mysqli object at "Start point", and use it to commit or rollback at "End point" the database query of class A and class B?
Do I have to create outside, and pass the same $mysqliObject to the __constructor() of class A and class B, and use that to rollback or commit (outside).
Would like to see more techniques with pro-con's.
Each database connection object you have represents a different connection to the database, even if the connection is to the same database server. Each connection is completely independent of each other, so a transaction started in one connection will be invisible to the other connection.
If all your database access is to the same server, then just create a single connection and pass it to anything that needs access to the connection. Then the transaction will be application-wide.
class ClassThatNeedsDbAccess
{
protected $connection = NULL;
protected function doQuery ($query)
{
return ($this -> connection -> execute ($query));
}
public function __construct (PDO $connection)
{
$this -> connection = $connection;
}
}
class OtherClassThatNeedsDbAccess extends ClassThatNeedsDbAccess
{
}
$myDb = new PDO ('dsn_goes_here');
$obj1 = new ClassThatNeedsDbAccess ($myDb);
$obj2 = new OtherClassThatNeedsDbAccess ($myDb);
If you're talking to different database servers, then you probably should wrap all the connections in an object that can coordinate the database activity. It will be that object's responsibility to track which database connection has a transaction in flight.
Use one MySQL object - connection for all your business logic... take a look at singleton pattern. You can pass it to constructor or you could use static class method to get mysql object.
Then it's a matter of you where you start end transaction, connection must be the same. Best would be abstract it into some general database access layer.

Check if a specific mysql connection already exists during a php script?

I have a class for each database table object. Each class takes care of connection/queries/data format (database table specific logic) as I prefer to assign a connection for each class for better modularity. Also, I am using specific connections for some tables and queries.
My question is how can I check if a connection already exists so it won't start another one?
Basically I want to check if the connection with the same username/password/database has already been made. Or is this not necessary because mySql won't start a new connection for the same user? If that is so then please explain.
After further research found out that this is not possible ... it would require to get the thread id and use the conn with the thread id and it would act like a persistant conn; Or it would require to keep track every thread ids used during a script and it's kinda useless.
There isn't yet a spimple method of doing this ... persistant connection can be a choice but then u have to take care of connection clean up and again sux :)
P.S. : Eventually i did made a tracking system for the connections during my app run (the guys that said to store them in a globally available object we're right). Stored conn object and conn params in 2 arrays in a singleton class object, checked if the params allready exists in params array , if not make new conn , if yes get the conn object from the array-key thats the same with the key where params were found... I allready had the arhitecture made for this but didnt want to use that class...not the logic i began with :), and also wanted to make it in a library item, thats why i was looking for a simple and abstract solution but there is only a particular solution :)
Create new connection for each class is not a good idea. It may be modularized to you but your mysql server will be soon bloated with too may connections error.
I suggest use singleton pattern and some OO.
class Singleton{
private static $instance=null;
public function connection(){
if(self::$instance==null){
self::$instance = mysql_connect(); // define it in your way,
}
return self::$connection;
}
}
class TableA extends Singleton{
function find($id){
$query="select * from `A` where `id`='$id'";
mysql_query($query, $this->connection());
... // other codes
}
}
I suggest you read this - I think this will help you http://www.php.net/manual/en/features.persistent-connections.php
If you must have a different connection for each class, you can use a static class property to hold the connection, and use the singleton pattern to retrieve it.
class SomeTable {
// Connection resource as a static property
// Since it is static, it will be shared by all instances of class SomeTable
public static $db = FALSE;
// Static method to retrieve the connection
// or create it if it doesn't exist
public static function get_conn() {
if (self::$db) {
// Just return the connection if it already exists
return self::$db;
}
else {
// If it doesn't already exist, create it and return it
self::$db = mysql_connect(...);
return self::$db;
}
}
// In other methods, use self::get_conn()
public function someQuery() {
$sql = "SELECT col FROM tbl";
// Call the connection singleton explicitly
// as the second param to mysql_query()
$result = mysql_query($sql, self::get_conn());
}
}

How do you efficiently connect to mysql in php without reconnecting on every query

I'm pretty sick of having to rewrite my code every time I learn something new about php (like the fact that mysql connections cannot be passed around in a session as a handle).
How do you implement mysql connection in your projects? A lot of people have proposed "connection pooling", but after reading the manual i'm still lost. It's like: "connection pooling is mysql_pconnect!" - me: "and...? how is that any different in reality? can you pass around a mysql_pconnect in a session? why is this seemingly mysterious aura??"
Let me explain my situation. I have a function called "query1":
function query1($query)
{
$db = new mysql(HOST,USER,PASS,DBNAME);
$result = $db->query($query);
$db->close();
return $result;
}
This is seems like a squanderous and inefficient way of querying a db (especially since you need a mysql handle for functions like mysql_real_escape_string). What is the correct form to do it? Can someone please help me?
Thank you I'd really appreciate a good honest answer.
Normally connections happen once a page load. AKA
class Database{
public function connect()
{
$this->connection = mysql_connect();
}
// This will be called at the end of the script.
public function __destruct()
{
mysql_close($this->connection);
}
public function query($query)
{
return mysql_query($query, $this->connection);
}
}
$database = new Database;
$database->connect();
$database->query("INSERT INTO TABLE (`Name`) VALUES('Chacha')");
Basically, you open the connection in the beginning of the page, close it at the end page. Then, you can make various queries during the page and don't have to do anything to the connection.
You could even do the mysql_connect in the constructor as Erik suggest.
To use the above using global variables (not suggested as it creates global state), you would do something like
Global $db;
$db = new Database;
// ... do startup stuff
function doSomething()
{
Global $db;
$db->query("Do Something");
}
Oh, and no one mentioned you don't have to pass around a parameter. Just connect
mysql_connect();
Then, mysql_query will just use the last connection no matter what the scope is.
mysql_connect();
function doSomething()
{
mysql_query("Do something");
}
Per the comments:
I think you should use mysql_pconnect() instead of mysql_connect(), because mysql_connect() doesn't use connection pooling. – nightcoder
You might want to consider whether you use mysql_connect or mysql_pconnect. However, you should still only connect once per script.
You don't need to connect to the database in every function. You need to connect to the database when the script starts running and save connection object in global state. In any function you can use that connection object to query the database. Your connection object will be recreated for every time script is executed but it will be very fast, because special connection pool is used behind the scene, so connection will be done immediately (matter of microseconds, because actually connection was not even broken, it was saved in connection pool).
Here is the example you asked for:
// this code should be executed on every page/script load:
$adoConn = ADONewConnection(...);
$adoConn->PConnect(...);
// ...
//And then in any place you can just write:
global $adoConn;
$adoConn->ExecuteNonQuery("INSERT INTO test SET Value = 'Hello, world!'");
As for your question "how do I implement connection pool". You don't. It's maintained by the server behind the scene and used if you (or the PHP library for work with PHP) use mysql_pconnect() function.
PS. If you are afraid to keep $adoConn as a global variable (I'm not) then you can create a class with a static property:
class DB
{
public static $adoConn;
}
// ...
DB::$adoConn->ExecuteNonQuery(...);

Categories