Calling protected variables in a class in another script - php

Am new to PDO, I have constructed the class as shown below. At Some point, I want to use the private variables in another script which requires this dbconnection.php
dbconnection.php
<?php
class DBconnect
{
private $host = 'localhost';
private $username = 'db_user';
private $password = 'pass';
private $dbname = 'db';
protected function connect()
{
$config = 'mysql:host='.$this->host.';dbname='.$this->dbname;
$pdo = new PDO($config, $this->username, $this->password);
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
return $pdo;
}
}
user.php
<?php
require dbconnection.php
//how can I call the private variables here?

Related

How to call a local property using OOP?

I'm a beginner looking to learn more about PHP OOP, so there are things I don't know, in the ShowUsers() method, I would like to display all users in the database, but I'm not getting it, because I don't know how to call the connection property. If I've been using encapsulation, it would be easy, but I'm using the connection property as local, and I really don't know how to call this property, how can I call it without using encapsulation?
db.php
<?php
class DbConnect {
private $host = 'localhost';
private $dbname = 'database';
private $username = 'root';
private $password = '';
public function __construct() {
try {
$conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception) {
throw new Exception($exception->getMessage());
}
}
}
main.php
<?php
require_once 'db.php';
class Main extends DbConnect {
public function __construct() {
parent::__construct();
}
public function ShowUsers() {
$sql = "SELECT * FROM users";
$result = parent::__construct()->prepare($sql); //Problem here
$result->execute();
}
}
$object = new Main();
$object->ShowUsers();
Note: I don't want to use encapsulation to make it work, I want to learn how to call the variable without using encapsulation, if possible.
Based on the code above and the comments, I recommend that you declare $conn as protected in your DbConnect class:
<?php
// db.php
class DbConnect {
private $host = 'localhost';
private $dbname = 'database';
private $username = 'root';
private $password = '';
protected $conn;
public function __construct() {
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception) {
throw new Exception($exception->getMessage());
}
}
}
Then in main.php, you can do:
<?php
// main.php
require_once 'db.php';
class Main extends DbConnect {
public function __construct() {
parent::__construct();
}
public function ShowUsers() {
$sql = "SELECT * FROM users";
$result = $this->conn->prepare($sql);
$result->execute();
}
}
$object = new Main();
$object->ShowUsers();
?>

PHP pass values from php file to other class

I am using a config.php file for a simular something, im trying to put my mysql credentials in there to then use them in a different file, but it does not pass the values,
is there any1 that could help me find a solution.
code config.php:
/* Database credentials*/
$dbHost = 'localhost';
$dbName = 'xx';
$dbUsername = 'xx';
$dbWachtwoord = 'xx';
code dbconnect.php:
<?php include 'config.php';
class Database
{
private $host;
private $db_name;
private $username;
private $password;
public $conn;
public function dbConnection()
{
$this->host = $dbHost;
$this->db_name = $dbName;
$this->username = $dbUsername;
$this->password = $dbWachtwoord;
$this->conn = null;
try
{
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $exception)
{
echo "Connection error: " . $exception->getMessage();
}
return $this->conn;
}
}
Class.user dbconnection:
<?php
require_once('config.php');
require_once('dbconnect.php');
class USER
{
private $conn;
public function __construct()
{
$database = new Database();
$db = $database->dbConnection();
$this->conn = $db;
}
public function runQuery($sql)
{
$stmt = $this->conn->prepare($sql);
return $stmt;
}
Thanks in advance =)
Rather than thinking about it as passing variables, think about it as passing configuration. It is necessary for your Database class to be aware of those configuration options in order for it to be used. In other words: once you create an instance of class Database it should be configured and ready to use, just like any service would.
I strongly suggest you follow the rule of injecting the configuration as a dependency.
Include 'config.php inside your class
public function dbConnection()
{
include 'config.php';
$this->host = $dbHost;
$this->db_name = $dbName;
$this->username = $dbUsername;
$this->password = $dbWachtwoord;
$this->conn = null;
try
{
You can try below code :
config.php
<?php
return [
'dbHost' => 'localhost',
'dbName' => 'xx',
'dbUsername' => 'xx',
'dbWachtwoord' => 'xx',
];
user class
class USER
{
private $conn;
private $config;
public function __construct()
{
$this->config = include "config.php";
$database = new Database(this->config['dbHost'], $this->config['dbUsername'], $this->config['dbWachtwoord'], $this->config['dbName']);
//...
}
//...
}
dbconnect.php
public function dbConnection($dbHost, $dbName, $dbWachtwoord, $dbName)
{
//....
}

Make Connections From One Location In PHP

In JSP, if I want to connect to the database, I would create a Java class called DBManager with the following code:
public class DBManager {
private final static String DB_URL = "jdbc:mysql://localhost:3306/mydb";
private final static String DB_USERNAME = "root";
private final static String DB_PASSWORD = "root";
public static Connection conn = null;
private static Statement stmt = null;
/**
* Tests connection with the database by getting connection using the
* database url and username and password. And creates a dumb statement and
* closes it to make sure everything is working fine.
*/
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);
stmt = conn.createStatement();
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Now, I can easily do something like:
PreparedStatement pstmt = DBManager.conn.prepareStatement("SELECT * FROM USER");
I'm learning PHP on my own and "most" of the online tutorials actually don't teach how to do stuff the right way.
They all do it in the traditional way in each page they need a connection:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Which is wrong because say you want to change your password? Then, you have to change it in each and every page that you used that password.
My question: how can I do something equivalent/similar to that Java class in PHP?
EDIT:
<?php
class DBManager {
public static $conn = null;
private static $hostname = "localhost";
private static $username = "root";
private static $password = "root";
private static $dbname = "tutorme";
protected function __construct() {
try {
DBManager::$conn = new PDO("mysql:host=localhost;dbname=tutorme", DBManager::$username, DBManager::$password);
DBManager::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
public static function getInstance() {
if (null === DBManager::$conn) {
DBManager::$conn = new DBManager();
}
return DBManager::$conn;
}
}
?>
I searched a little and came up with the above code. However, now when I call
$stmt = DBManager::getInstance()->prepare("INSERT INTO SUBJECT (SubjectTitle, SubjectName) VALUES (:subject,:subj)");
I get an error that there's no function in DBManager called prepare()
which means that my DBManager::getInstance is returning a DBManager object instead of PDO conn object
You have one obvious mistake:
DBManager::$conn = new DBManager();
You don't want that!
Just call new DBManager() and the $conn var will be initiliazed!
And also here are some minor changes,
<?php
class DBManager {
public static $conn = null;
private static $hostname = "localhost";
private static $username = "root";
private static $password = "root";
private static $dbname = "tutorme";
private function __construct() {
try { // why not using $hostname and $dbname?!
DBManager::$conn = new PDO("mysql:host=" . DBManager::$hostname . ";dbname=" .DBManager::$dbname, DBManager::$username, DBManager::$password);
DBManager::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
}
// getConnection() is a better naming of the function
public static function getConnection() {
if (is_null(DBManager::$conn)) {
new DBManager();
}
// now $conn is initialized
return DBManager::$conn;
}
}
?>
make a class just like what's in java and let's call it database.php
in the file u want to do something
include 'database.php';
.... your code .....
tip: look for "autoload"
is that what you want?
you can start off with this
class Database{
function __construct(){
$this->host = HOST;
$this->user = DB_USER;
$this->pass = DB_PASS;
$this->db = DATABASE;
$this->con = $this->connect();
}
function connect(){
$q = mysqli_connect($this->host, $this->user, $this->pass, $this->db);
if($q) return $q; die("Couldn't Connect to Database");
}
}
other file like so for example
include 'Database.php';
$db = new Database();
mysqli_query($db->con, "QUERY_HERE");

PHP - PDO Connection Class - Unable To Access

I'm currently writing my first PHP application using OOP and PDO. In doing so I'm working on a connection class so that I can initiate a database connection when needed. I believe the terms for the way I am doing it is dependency injection.
I currently have an error when trying to access the connection.
This is my connection class:
class db{
private $host = '';
private $dbname = '';
private $username = '';
private $password ='';
public $con = '';
function __construct(){
$this->connect();
}
function connect(){
try{
$this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password);
$this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'We have a problem!';
}
}
}
And this is how I am trying to call it inside of other classes.
private $con;
public function __construct(db $con) {
$this->con = $con;
}
However this is the error I receive when trying to run it.
Catchable fatal error: Argument 1 passed to users::__construct() must be an instance of db, none given.
Any advice on what I am doing incorrectly would be much appreciated.
You will need to first create your DB instance and pass it to the constructor of your 'Other' class
$db = new DB();
$class = new OtherClass($db);
Apart from that, there are other issues:
The DB class constructor did not assign values to the database name, user and password etc. One way of doing it is to pass those settings to the constructor of DB and assign the values to the private properties.
class DB{
private $host = '';
private $dbname = '';
private $username = '';
private $password ='';
public $con = '';
function __construct($host, $dbname, $username, $password){
$this->host = $host;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
$this->connect();
}
function connect(){
try{
$this->con = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->username, $this->password);
$this->con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo 'We have a problem!';
}
}
}

How to combine multiple php/mysql database connections?

When building dynamic websites I use a php include to make my connections to the database. The included file is very basic:
mysql_connect($hostname = 'host', $username = 'user', $password = 'password');
mysql_select_db('database');
This works fine.
In some places I use an AJAX system to create drag-and-drop reordering of database records which updates the database at the same time, it is adapted from something I found on the internet. This uses its own connection code:
class SortableExample {
protected $conn;
protected $user = 'user';
protected $pass = 'password';
protected $dbname = 'database';
protected $host = 'host';
public function __construct() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn);
}
This also woks fine.
What it means, however, is that I have to add the username, password, host and database to two separate files. Sometimes the second one is forgotten and causes the website to fail.
My question is, how can I either combine both connection files into one, OR how can I get the second block of code to accept external variables so that I only have to enter the actual values in one place?
Your last question is easy.
db.config.php
$host = '';
$user = '';
$pass = '';
$db = '';
db.plain.php
include 'db.config.php';
$conn = mysql_connect($host, $user, $pass);
mysql_select_db($db,$conn);
db.class.php
include 'db.config.php';
class SortableExample
{
protected $conn;
public function __construct()
{
global $host, $user, $pass, $db;
$this->conn = mysql_connect($host, $user, $pass);
mysql_select_db($db,$this->conn);
}
}
You can simply put the database connection code in another file and included that everywhere you need it.
Create a single entry point for your database connection.
Use a Singleton with lazy instantiation for that:
class ConnectionProvider {
protected $conn;
protected $user = 'user';
protected $pass = 'password';
protected $dbname = 'database';
protected $host = 'host';
private static $__instance;
private function __construct() {
$this->conn = mysql_connect($this->host, $this->user, $this->pass);
mysql_select_db($this->dbname,$this->conn);
}
public static function getInstance() {
if ( self::$__instance == null) {
self::$__instance = new ConnectionProvider();
}
return self::$__instance;
}
public function getConnection() {
return $this->conn;
}
}
And then, from your code
ConnectionProvider::getInstance()->getConnection();
to use the connection wherever you need it.
SortableExample would thus become:
class SortableExample {
protected $conn;
public function __construct() {
$this->conn = ConnectionProvider::getInstance()->getConnection();
}
...
}

Categories