I've been taking help from this site by years now, but I have never asked, so this is my first question here. This is a theoretic question, I would like to know if I'm thinking in the right way.
First of all, sorry for my English.
I was thinking if I could simplify my existing MySQL object.
For the connection, I use the singleton pattern to ensure that my app connect only one time during script execution. So, in every class, when I want to use MySQL, a get the instance.
$db = db::getInstance();
$result = $db->query("SELECT * FROM data;");
the $result is a dbResult class, on which I can use loops (while($row = $result->nextRow()) {...}), jump to row number, etc...
after all things are done, then I $result->free(); the result class.
Question 1.
Why not return an associative array instead of the dbResult class? I could use a foreach on it. But what about a result with 1.000.000 rows?
Question 2.
Do I have to get the instance every time I want to use SQL?
class db{
...
private static $instance;
...
public static function query($_query){
if (!self::$instance){
self::$instance = new db(); //first we need to connect...
self::query($_query);
}
else{
//execute query, then load result in array,
//or in case of insert, return insert_id
return $return_array;
}
}
In this case, I can simply call a query from anywhere in my code without instantiating the db object.
$result = db::query("SELECT * FROM data;");
...
//or insert
db::query("INSERT INTO test VALUES ('test_value');");
Would be this a bad practice?
Have a look at Doctrine 2 for example. It's a big project, but when u master it, its damn awesome :)
If this is too big for you, refactor your class to not use singleton pattern implementation and/or static methods.
Here's what I suggest:
Create a Database class.
<?php
class Database {
private $db;
private $host;
private $username;
private $password;
// Should probably not put your DB credentials here, just the Databases, but its just here for this purpose
const DB_HOST = '';
const DB_USERNAME = '';
const DB_PASSWORD = '';
// Just an example of having multiple databases, so you can just Database::DB_1 when you need to use them
const DB_1 = '';
const DB_2 = '';
const DB_3 = '';
public function __construct($db = self::DB_1, $host = self::DB_HOST, $username = self::DB_USERNAME, $password = self::DB_PASSWORD) {
$this->db = $db;
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->db = $db;
}
// So if you have different databases, you can create different functions to connect to them via PDO drivers
public function connectToMySQL($db = null) {
$dsn = is_string($db) ? 'mysql:dbname='.$db.';host='$this->host : 'msql:dbname='.$this->db.';host='$this->host;
try {
return new PDO($dsn, $this->username, $this->password);
} catch (PDOException $E) {
echo 'Connection error: '.$E->getMessage();
exit();
}
}
To use this you would just:
/*
* Remeber how we defined it? you can leave the parameters blank, or pass in things
*/
$Database = new Database();
/*
* A PDO database object that connects to your database automaticaly.
* You can also passin Database::DB_2 sand stuff if you hav multiple databases.
* is a PDO object, so to use it, just look up PHP's PDO tutorials
*/
$PDO = $Database->connectToMySQL();
/*
* Then to end the connection, it's just as simple as setting it to null
*/
$PDO = null;
This way, you create 1 Database object that can generate MANY connections, it's like a Factory class.
I believe this why is most versatile, but I'm always open to suggestions as well.
Related
I work now on a project for learning i'm a beginner, i use singelton pattern to make sure, that there is just one PDO connection to the Data Base.
I made var_dump() for all possible connections to the Mysql and I founded that there were 10 objects of pdo.
how can i found, from where come all this objects?
Are 10 objects of PDO normal?
I wanted to use just one for all the project.
my singelton
<?php
namespace App\Database;
use PDO;
use PDOException;
class DataBase
{
private static $instance;
private PDO $pdo;
private function __construct()
{
try {
$db= parse_ini_file("..//..//..//config.ini");
#$db= parse_ini_file("..//..//config.ini");
$type = $db['type'];
$host = $db['host'];
$name = $db['name'];
$user = $db['user'];
$password = $db['password'];
$this->pdo = new PDO($type . ':host=' . $host . ';dbname=' . $name, $user, $password);
}
catch (PDOException $e) {
echo "there is an error";
die();
}
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
return $this->pdo;
}
private function __clone() {}
public static function getInstance()
{
if (!static::$instance) {
static::$instance = new DataBase();
}
return static::$instance;
}
public function getPdo(): PDO
{
return $this->pdo;
}
}
?>
then I made a connection of the pdo like this in another place, where I need to use pdo init.
function makePdo(){
$db= DataBase::getInstance();
$pdo= $db->getPdo();
var_dump($pdo);
return $pdo;
}
Thank you
10 connections are not normal, you should have only one to one DB. Use singleton pattern for database connection:
https://gist.github.com/jonashansen229/4534794
https://phpenthusiast.com/blog/the-singleton-design-pattern-in-php
In this case, you will have one connection and always will get it instead of creating duplicates.
No it should be only one object, the best way to implment it, is to create a separate config file to store your DBMS credentials as associative array, like mysql db_name, host .. etc.
Then create a config class to get those values by creating a static get method.
and finally use this get method inside your db constructor to get the credentials.
I'm pretty sure If you do that you'll get only one instance of PDO object.
I've started learning PHP OOP a few days ago and while I know the main functionality about it, I decided it would be much more efficient and clean to make the class called DB. It would handle the connection between the database using PDO.
Well, I didn't find any information similar to what I'd like to achieve. That's the main purposes of this class:
Store all the instances of the database and the connection info
Be able to require a single file which contains all the created statements
Using different methods for each possible situation, so you wouldn't need to write the same stuff every time*
*What I mean by saying 'writing the same stuff' is that you could create a few different methods:
public function prepareExecFetch() { ... }
public function prepareExec() { ... }
public function fetch() { ... }
And somehow I'd like to connect these functions with the PDO methods, so I wouldn't need to write these lines again and again for every statement I created. DB class would take care of this stuff with the single method, depending what I'd like to achieve.
I try to avoid writing these lines by using DB:
$stmt1 = $pdo->prepare('...');
$stmt1->execute(array('...' => '...'));
$r1 = $stmt1->fetch(PDO::FETCH_ASSOC);
My project is getting bigger and bigger, so, in my opinion, that's a terrible idea to put these lines of code every time I need to access some information from the database.
Currently, I'm not interested in the PHP frameworks. Before using them, I'd like to understand how it works.
Also, could you give me some ideas about the written code of mine?
<?php
class DB {
private $host;
private $name;
private $charset;
private $user;
private $password;
// I wonder how could I store all the statements here, using the
// $stmt1 = ...; $stmt2 = ...; wouldn't work cause it has to be
// dymanic and I can't create new properties inside any method
private $stmts = [];
// Not using static modifier because if there was another instance of this
// class, then $stmtCounter variable should be resetted again to 0
private $stmtCounter = 0;
public function __construct(string $host, string $name, string $charset,
string $user, string $password) {
$this->host = $host;
$this->name = $name;
$this->charset = $charset;
$this->user = $user;
$this->password = $password;
$pdo = new PDO('mysql:host=' . $this->host . '; dbname=' . $this->name . ';
charset=' . $this->charset, $this->user, $this->password);
// While false, PDO will take advantage of the native prepared statement
// functionality
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
// In exception mode, if there is an error in SQL, PDO will throw
// exceptions and the script will stop running
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function execPrepareFetch(string $stmt, array $execParams) {
$this->stmtCounter++;
array_push($this->stmts, $stmt . $this->stmtCounter);
}
}
// This way by instantiating multiple instances we can easily work with multiple
//databases
$db = new DB(DB_HOST, DB_NAME, DB_CHARSET, DB_USER, DB_PASSWORD);
$db->execPrepareFetch('$stmt', [':user' => 'some value']);
This code isn't completed, it's just my starting ideas how could I do this. I'd to get your ideas if it's worth doing that and is it even possible? Thank you :)
I am looking into oop to enhance my web dev knowledge. At the moment i am having a bit of an issue. I have created a Database class that contains all the queries etc. ( fetch, count, etc. ) This just allows the queries or updates to take up less space in the other classes etc. The issue i am having is passing this class along and making it globally accessible. I have used
global $db;
within a class function but i read that it is bad practice to use that. I also do not want to pass the $db variable as a parameter if i did i would have to change a lot of my current classes and it would just be easier if i can make $db globally available in a "good" practice way.
I can provide my Database class if necessary it is just a simple class with the variable that initiates the connection through construct.
( Second Question )
I was also reading about the singleton instance function, before implementing i read that it was also considered bad practice. Is there something that should take its place?
( I decided to place the class below )
class Database {
private $host = 'localhost';
private $user = 'xxx';
private $pass = 'xxxx';
private $dbname = 'xxxxx';
private $dbh;
private $error;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
// Set options
$options = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
PDO::ATTR_EMULATE_PREPARES => false
);
// Create PDO Instance
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
public function fetch($sql, $param = "") {
$this->stmt = $this->dbh->prepare($sql);
if (empty($param)) {
$this->stmt->execute();
} else {
$this->stmt->execute($param);
}
return $this->stmt->fetch();
}
}
$db = new Database();
An example of what i am attempting to accomplish is as follows
( User Profile Page )
class User {
function set_user($input) {
if (is_numeric($input)) {
$this->user = $input;
} else {
$user = $db->fetch("SELECT userid FROM users WHERE url=:url", array(':url' => $input));
$this->user = $user['userid'];
}
}
}
AN issue with your approach is not the global var, but that the DB connection is permanently open. It is not always good. This is kind of service object and it is not partiularily bad to have it globally visible.
If you don't want it global and want to solve the open connection issue, you can easily close the connection in the destructor, remove the global var and create/use/delete the DB wherever required.
Another approach would be not to use singleton, but so called service class. Make DB class not instanciable, but rather define service methods (all static) - open, close, execute, or whatever. Than the DB clients should not create DB object, but only access the static methods. This approach fits the reality very nice, as DB-access is seen as a service.
Today i tried to convert my functions to PHP Class.
I tried with some basic steps.
<?php
class DataBase {
private $host;
private $user;
private $password;
private $db;
private $mysqli;
function __construct() {
$this->host = "localhost";
$this->user = "root";
$this->password = "";
$this->db = "my_database";
$this->mysqli = new mysqli($this->host, $this->user, $this->password, $this->db);
}
function __destruct() {
$this->mysqli->close();
}
public function query($query, $params = '', $bind_result) {
$stmt = $this->mysqli->prepare($query);
//Need to change this to process the array of params
$stmt->bind_param('i', $params);
$stmt->execute();
//Change this to handle array of bind
$stmt->bind_result($bind_result);
//Loop the result and store it in a temp array
$stmt->fetch();
//Don't print the statement. Just close the statement and return the array.
printf("%s\n", $bind_result);
/* close statement */
$stmt->close();
}
}
?>
I have to now create another class. I created one dummy table in database.
<?php
class Dummy {
private $database;
function __construct() {
$this->database = new Database();
}
public function getAllDummy() {
$query = "SELECT name FROM dummy WHERE id = ?";
$this->database->query($query, 1, 'name');
}
}
?>
But I don't think it is the right way to do the things. I feel some of them are in the right way and some of them are wrong.
When i call the query() function, Do i need to connect the database all the time in every classes' construct method? Or Do i need to change the Database class to static functions? So i can call the functions like Database::query();
It seems i need to create everything from the start. Is such a model already available in internet? like cakephp, codeigniter
I would like to recommend you to read something about ORM for PHP. For example I m using Doctrine 2 (http://www.doctrine-project.org/) It is kinda complex but definitely worth to learn. There is everything you are trying to code already done, so why you should make it again?
In your OOP principe there are some mistakes.
You are creating Database instance for every class like Dummy, if you will have class Users, Articles, you will create 3x Database, it isnt really good. You should make Database as service or Singleton and make it just once. Good solution for this can be Dependency injection (http://en.wikipedia.org/wiki/Dependency_injection).
Also I would recommend you to generalize whole Dummy class, to make it more general. Dont make method "getAllDummy" but for example "getAll($tableName)"so you can use it for every table.
Besides Doctrine (Which is powerfull and almighty already but still to complex) I can suggest you db.php (http://dbphp.net) which does everything what doctrine but is single file and is very easy to use. Cons: It is not well documented yet and has no big community yet.
So I started this tutorial as an intro to the PHP PDO. Up until now I've only worked with basic mysql_* type queries.
I've noticed that, throughout the tutorial, the connect -> do action -> disconnect pattern is repeated, and only the do action part ever changes.
In a real-world setting, would it be a good idea to eliminate repetition by creating a function into which queries can be passed?
For example:
a function for handling queries:
<?php
function databaseDo($action) {
$db_hostname = 'localhost';
$db_username = 'root';
$db_password = 'root';
try {
// Establish DB connection
$dbh = new PDO("mysql:host=$hostname;dbname=mysql",
$db_username, $db_password);
echo 'Connected to database';
// Do something
$action($dbh); // <- here goes whatever action we wish to perform
// Close connection
$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
Then, suppose I want to perform the action in the first example of the PDO tutorial, I would set it like this:
<?php
// Define action
$insert = function($dbh) {
$query = "INSERT INTO animals(animal_type, animal_name)
VALUES ('kiwi', 'troy')";
$exec = $dbh->exec($query);
echo $exec;
};
// Perform action
databaseDo($insert);
?>
Scope of $dbh
I am using $dbh as an argument. Is this the proper way of passing a variable to a function like this without making it global?
Yes it is a great idea
A suggestion would be making a database helper class that uses the singleton pattern.. Something like
abstract class DB
{
protected static $instance;
protected $db;
protected static $host = 'host';
protected static $user = 'user';
protected static $pass = 'pass';
protected static $database;
public static function getInstance()
{
if (!isset(self::$instance)) self::$instance = new static();
return self::$instance;
}
public static function doStatement($statement, array $parameters)
{
$handler = self::sql()->prepare($statement);
$handler->closeCursor();
$handler->execute($parameters);
return $handler;
}
protected function __construct()
{
$this->db = new PDO(sprintf('mysql:host=%s;dbname=%s', static::$host, static::$database), static::$user, static::$pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
}
public static function db()
{
return static::getInstance()->db;
}
}
class Main extends DB
{
protected static $database = 'db';
}
to use it
$db = Main::getInstance();
$results = $db::doStatement('SELECT * FROM table WHERE id = :id', array(':id' => 5));
Now this is just very basic, and much more would need to be added (Exception handling, more/better helper methods, etc) but I have used something like this in many projects.
While avoidance of repetition (DRY) is a principle that should always factor into your coding decisions, it should be achieved without violating another important principle, which is the separation of concerns (SoC, see also SRP). Your example, databaseDo($action), is dual-purpose: it (1) instantiates a database connection and (2) it executes a query.
Now, you may say, 'Yes! That is just what I want! Kill two birds with one stone!', and your reason for saying so would be understandable. However, mixing responsibilities can become problematic because, when you have to make changes to the way in which one responsibility is handled, you will likely also have to make changes to the way the other responsibility is handled.
Imagine you were, at some point down the road, required to support two database connections instead of just one. Suppose one of the two databases supports transactions on tables for which the other does not. Your databaseDo() function will first have to negotiate to which database to connect, and then, in order to safely execute the 'do' action, some transaction support testing will be required. It would look something like this:
$context = 'production'; // but it could equally be 'development'
function databaseDo($action) {
$db_hostname = ($context == 'production') ? 'http://remotehost.com' : 'localhost';
$db_username = ($context == 'production') ? 'apache' : 'root';
$pass = ($context == 'production') ? 'productionpassword' : 'developmentpassword';
try {
$dbh = new PDO("mysql:host=$db_hostname;dbname=mysql", $db_username, $db_password);
echo 'Connected to database';
if($context == 'production') {
// ... some complicated logic to determine whether the production db
// will support your query, then execute it if so, exit if not ...
}
if($context == 'development') {
// ... some more complicated logic for testing and querying the
// development db ...
}
$dbh = null;
} catch(PDOException $e) {
echo $e->getMessage();
}
}
Additional requirements for handling one responsibility will add complexity to the handling of the second responsibility, and this function will become more and more difficult to maintain.
A better approach to DRY in this scenario would be to handle database connection management in one component, such as within a context-aware singleton class instance (a common approach), and the query handling in another component. Thus, your query function would not necessarily have to change on account of a change in database connection handling. The tutorial to which you referred has instructions for creating and using such a singleton instance.