I am trying to connect to a database (MySQLi) just once, but am having problems doing so.
How do I make a connection global for the entire script? There are multiple files (index.php, /classes/config.class.php, /classes/admin.class.php, etc).
I've tried the following:
In: config.class.php
public static $config = array();
public static $sql;
function __construct() {
// database
db::$config['host'] = 'localhost';
db::$config['user'] = '_';
db::$config['pass'] = '_';
db::$config['db'] = '_';
// connect
db::$sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
}
Again, in config.class.php
public function contectToDatabase($sql){
$sql = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
$this->sql = $sql;
}
I use the class with the following code:
$config = new db();
I really am puzzled at how I'm to do this. Can anyone help?
--- Edit ---
This is my new config.class.php file:
public static $config = array();
public static $sql;
private static $db;
private $connection;
public function __construct() {
// database
db::$config['host'] = '_';
db::$config['user'] = '_';
db::$config['pass'] = '_';
db::$config['db'] = '_';
// connect
$this->connection = new mysqli(db::$config['host'], db::$config['user'], db::$config['pass'], db::$config['db']);
}
function __destruct() {
$this->connection->close();
}
public static function getConnection() {
if($db == null){
$db = new db();
}
return $db->connection;
}
And this is how I'm loading it:
require_once("classes/config.class.php");
$config = new db();
$sql = db::getConnection();
However, running a real_escape_string results in the following errors:
Warning: mysqli::real_escape_string() [mysqli.real-escape-string]: Couldn't fetch mysqli in /home/calico/_/_.com/_/index.php on line 20
Warning: mysqli::query() [mysqli.query]: Couldn't fetch mysqli in /home/calico/_/_.com/_/index.php on line 28
Personally, I use a singleton class. Something like this:
<?php
class Database {
private static $db;
private $connection;
private function __construct() {
$this->connection = new MySQLi(/* credentials */);
}
function __destruct() {
$this->connection->close();
}
public static function getConnection() {
if (self::$db == null) {
self::$db = new Database();
}
return self::$db->connection;
}
}
?>
Then just use $db = Database::getConnection(); wherever I need it.
Related
I have changed the code a bit, but nothing changed. I'm obviously not able to use PDO correctly.
So, the db connection is here:
class Database {
private static $instance = null;
private function __construct(){}
public static function getInstance() {
if(is_null(self::$instance)){
self::$instance = new PDO("mysql:host = localhost, dbname = elektro", "root", "");
}
return self::$instance;
}
}
This class deals with the DB:
abstract class ActiveRecord {
private static $conn;
public static function getAll(){
$conn = Database::getInstance();
$table = static::$table;
$class = get_called_class();
$q = self::$conn->query("select * from {$table}");
$res = $q->fetchAll();
return $res;
}
}
And this one is supposed to fetch all rows from one table:
class Autor extends ActiveRecord {
public static $table = "autor";
}
$conn = Database::getInstance();
$allAutors = Autor::getAll();
print_r($allAutors);
This results in Fatal error: Call to a member function query() on null
I'm just new using OOP php and I'm having a hard time to figure out how to query in database using class method. Here's my code and I've got an error which I dont know how to solve.
I declared the connection variable but I don't know why it's undefined
Notice: Undefined variable: connection
Fatal error: Call to a member function query() on a non-object in
db.php
class DbConnector {
private $serverName;
private $userName;
private $password;
private $dbName;
private $connection;
public function __construct(){
$this->serverName = "localhost";
$this->userName = "root";
$this->password = "attl";
$this->dbName = "oop";
$this->connection = new mysqli($this->serverName, $this->userName, $this->password, $this->dbName);
if ($this->connection->connect_error) {
$this->connection = die("Connection failed: " . $this->connection->connect_error);
}
}
public function getConnection(){
return $this->connection;
}
}
index.php
include('../queries/db.php');
class Users{
private $connection;
public function __construct(){
$con = new DbConnector();
$connection = $con->getConnection();
}
public function getUsers(){
$sql = $connection->query("SELECT * FROM login");
while($getUsers = $sql->fetch_array()){
echo $getUsers['username'];
}
}
}
$user = new Users();
return $user->getUsers();
Your problem is you're trying to access a locally scoped variable rather than a class property
public function getUsers(){
$sql = $connection->query("SELECT * FROM login"); // HERE
while($getUsers = $sql->fetch_array()){
echo $getUsers['username'];
}
}
this can't find a variable called $connection in that scope, you need to access the object property using $this.
class Users {
private $connection;
public function __construct()
{
$con = new DbConnector();
// Assign this to object propety declared above
$this->connection = $con->getConnection();
}
public function getUsers()
{
// now access the object property set in constructor.
$sql = $this->connection->query("SELECT * FROM login");
while($getUsers = $sql->fetch_array()){
echo $getUsers['username'];
}
}
}
Since you're declaring the following as private:
private $connection;
and including it in your construct:
public function __construct(){
$con = new DbConnector();
$connection = $con->getConnection();
You need to use $this for its property:
$this->connection = $con->getConnection();
Then changing:
$sql = $connection->query
to
$sql = $this->connection->query
in order to use the connection property.
Now, I have to admit that I am not an OOP expert and there may be another way to have solved this, yet this is what I took from it, which worked for me.
I'm fairly new to PHP; however I think I have a good grasp on it, but I'm having an issue understanding why I'm not able to access a second version of a connect to DB class I have.
I connect globally to the first one in a config script.
$db = new db(array('login info'));
I access it in my controller using the following.
public $db;
public function __construct(){
$this->db = $GLOBALS['db'];
}
Then inside my controller I have a function where I create a new instance of this to connect to a different DB.
$ordersDB = new db(array('login info'));
Now I would like to access each database separately using something like
$this->db->select("SELECT * FROM ada Yada
and
$ordersDB->select("SELECT * FROM Yada Yada
However both are still accessing the first db. I know this because I am getting a table does not exist error when executing the second one and it tells me what db its querying. However a var_export of them shows they are infact different! Am I completely misunderstanding how classes work or can I only have one DB open at a time? Thanks for any help.
Edit: Is this what you are looking for?
$db = new db(array(connect info));
$controller = new controller();
$controller->connectToSecondDB();
class db {
public $dbHost;
public $dbUser;
public $dbName;
public $dbPassword;
public function __construct() {
$arguments = func_get_args();
if(!empty($arguments)){
foreach($arguments[0] as $key => $property){
if(property_exists($this, $key)){
$this->{$key} = $property;
}
}
}
}
public function connect() {
if(!isset(self::$connection)) {
self::$connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
}
}
class controller {
public $db;
public function __construct(){
$this->db = $GLOBALS['db'];
}
public function connectToSecondDB(){
$ordersDB = new db(array(connect info));
$ordersDB->select("SELECT * FROM `customer` WHERE email = 'email#address.com' LIMIT 1")
$this->db->query("SQL");
}
}
EDIT Select Query Function
private function _sel($table, $where="", $order_by="", $limit="", $group_by="",$database = NULL){
if($database === NULL) { $database = $this->db; }
if($limit == 1){ $single = true; }else{ $single = false; }
//if( is_array($where) ){ $this->_buildWhere(); }
$where = (strlen($where) > 0) ? "WHERE $where " : $where;
$group_by = (strlen($group_by) > 0) ? "GROUP BY $group_by " : $group_by;
$order_by = (strlen($order_by) > 0) ? "ORDER BY $order_by " : $order_by;
$limit = (strlen($limit) > 0) ? "LIMIT $limit " : $limit ;
$sql = "SELECT *
FROM `$table`
$where
$group_by
$order_by
$limit";
// Debug
//if(INCLUDE_CHECK){ echo "<HR>".$sql."<HR>"; }
$results = $database->select($sql);
if($single && count($results) > 0 ){
return($results[0]);
}else{
return($results);
}
}
A solution is to reuse your $controller object. Add a query method. Then you can run the query separate from the connection method so the controller object can be reused
You need error checking each step along the way.
Note that I removed $this->db->query("SQL"); as I am not sure what exactly it did. The concept in the answer should get you started redesigning your class.
UPDATE
Based on comment, I changed the $db from a super global and passed it to the controller object.
$db = new db(array(connect info));
// try passing the $db object so it does not need to be a super global
$controller = new controller($db);
// You can keep your $controller object as a global - I use a session variable for this to reuse it between pages.
$controller->connectToSecondDB();
// run the query separate from the connection so the controller object can be reused
$result = $controller->ordersQuery("SELECT * FROM `customer` WHERE email = 'email#address.com' LIMIT 1")
class db {
public $dbHost;
public $dbUser;
public $dbName;
public $dbPassword;
public $connection;
public function __construct() {
$arguments = func_get_args();
if(!empty($arguments)){
foreach($arguments[0] as $key => $property){
if(property_exists($this, $key)){
$this->{$key} = $property;
}
}
}
}
public function connect() {
if(!isset($this->$connection)) {
$this->connection = new mysqli($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName);
}
}
class controller() {
public $db;
public $ordersDB; // ADD THIS
public function __construct($db2){
$this->db = $db2;
}
public function connectToSecondDB(connect info){
$ordersDB = new db(array(connect info));
}
public function ordersQuery($sql){
$this->ordersDB->query($sql)
// Fetch and return you result set here
}
}
I'm very new at PHP. I have two classes: Database and RetrieveItem. Because RetrieveItem needs a connection, I've just been extending the Database class to use its constructor. Apparently this is wrong, because RetrieveItem is not a database?
Here is my current code:
class Database {
public $host = '127.0.0.1';
public $username = 'root';
public $password = '';
public $dbname = 'example';
function __construct(){
$this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
}
}
class RetrieveItem extends Database {
function retrieve_item(){
$query = $this->connect->prepare("SELECT * FROM posts");
$query->execute();
$all_items = $query->fetchAll(PDO::FETCH_ASSOC);
return $all_items;
}
}
And on a separate page, to use this, I have:
include 'db.php';
$retrieve = new RetrieveItem();
print_r($retrieve->retrieve_item());
Rather than extend the class, how can I access the Database constructor in the cleanest possible way?
Any help or guidance would be much appreciated.
This amended code is still not working:
Argument 1 passed to RetrieveItem::__construct() must be an instance of Database, none given:
class Database {
public $host = '127.0.0.1';
public $username = 'root';
public $password = '';
public $dbname = 'example';
function __construct(){
$this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
}
}
class RetrieveItem {
private $_db;
public function __construct(Database $database){
$this->_db = $database;
}
public function retrieve_item(){
$query = $this->connect->prepare("SELECT * FROM posts");
$query->execute();
$all_items = $query->fetchAll(PDO::FETCH_ASSOC);
return $all_items;
}
}
In use:
include 'db.php';
$database = new Database();
$retrieve = new RetrieveItem($database);
print_r($retrieve->retrieve_item());
As you said, with plain dependency injection
class RetrieveItem {
private $_db;
public function __construct(Database $db) {
$this->_db = $db;
}
}
For easier use, you can abstract the injection in container, or, at least, has one super class that recieves the injection.
A dependency injection is so easy but it sounds complicated.
$class1 = new firstclass();
$class2 = new secondclass($class1); //This is a dependency injection.
class firstclass{
private $var1;
private $var2;
public function __construct(){
$this->var1 = "hello";
$this->var2 = "world";
}
public function getvar1(){
return $this->var1;
} //imagine a second one like this for var2;
}
class secondclass{
private $fc; //will hold first class object or the dependency.
public function __construct($firstclassobject){
$this->fc = $firstclassobject;
echo $this->fc->getvar1(); //call dependency methods like this.
echo $this->fc->getvar2();
} //echoes helloworld
}
So you pretty much put an object of one class and asign it to a field in your other class.
for your edit
set this line in your database class at the top.
public connect; //add to dbclass
Then do this in your function
public function retrieve_item(){
$connect = $this->db->connect; //added this
$query = $connect->prepare("SELECT * FROM posts"); //changed this
$query->execute();
$all_items = $query->fetchAll(PDO::FETCH_ASSOC);
return $all_items;
}
class Database {
public $host = '127.0.0.1';
public $username = 'root';
public $password = '';
public $dbname = 'example';
public $connect;
function __construct(){
$this->connect = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
}
}
class RetrieveItem {
private $connect;
public function __construct(&$db){
$this -> connect = $db -> connect;
}
function retrieve_item(){
$query = $this->connect->prepare("SELECT * FROM posts");
$query->execute();
$all_items = $query->fetchAll(PDO::FETCH_ASSOC);
return $all_items;
}
}
// Usage
$db = new Database();
$retrieve_item = new RetrieveItem($db);
Here in Retrieve Item we tried to send the database object as a refereneced variable rather sending a copy of its, which happens to be a good way of passing connection to your operable classes
I'm very new to php constructors I know of from java. But for some reason, this one is not running. I am following a tutorial and I can't seem to get a 1 only a 0. Here are the two scripts involved:
<?php
class dbConnection{
protected $db_conn;
public $db_name = 'todo';
public $db_user = 'root';
public $db_pass = '';
public $db_host = 'localhost';
function connect(){
try{
$this->db_conn = new PDO("mysql:host=$this->db_host;db_name=$this->db_name",$this->db_user,$this->db_pass);
return $this->db_conn;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
}
?>
<?php
include_once( 'class.database.php' );
class ManageUsers {
public $link;
public $constructed;
function ManageUsers(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
return $this->link;
}
function registerUser($username,$password,$ip_address,$time,$date){
$query = $this->link->prepare("INSERT INTO users (username,password,ip_address,time,date) VALUES (?,?,?,?,?)");
$values = array($username,$password,$ip_address,$time,$date);
$query->execute($values);
$counts = $query->rowCount();
return $counts;
}
}
$users = new ManageUsers();
echo $users->registerUser('bob','bob','127.0.0.1','10:00','29-02-2012');
?>
You should try to use __construct() instead for ManageUsers().
Also, I'm not quite sure what you're trying to achieve in your constructor (see my markings below):
function ManageUsers(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
return $this->link; //<--?
$constructed = 'Construct'; //<--?
}
Agreed - the method you are using is the old school PHP4 method - you should use the __construct() method as explained above.
http://www.php.net/manual/en/language.oop5.decon.php
This page explains construct() & destruct() in PHP 5
// constructor
function __construct() {
ManageUsers();
}
function ManageUsers(){
$db_connection = new dbConnection();
$this->link = $db_connection->connect();
$constructed = 'Construct';
}
You cannot return from a constructor tho, maybe you can want to make link a public property and access it directly like this
$user = new ManageUsers();
$link = $user->$link;