Reputation: 1645
I am new to php OOP. I have a question regarding my situation.
db.php
class db{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
public function __construct() {
$this->db_host="localhost";
$this->db_name="bs";
$this->db_user_name="root";
$this->db_pass="";
}
public function conn(){
try {
$conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $conn;
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
}
}
item.php
require "../../includes/db.php";
class item{
public $user_uid;
public $last_id;
public $display_item;
public $iid;
protected $item_id;
protected $item_name;
protected $item_price;
protected $item_quantity;
public function add_item($uid,$item_id,$item_n,$item_p,$item_q){
$db = new db();
$conn=$db->conn();
$this->user_uid=$uid;
$this->item_id=$item_id;
$this->item_name=$item_n;
$this->item_price=$item_p;
$this->item_quantity=$item_q;
try {
$sql = "INSERT item(uid,item_id,item_name,item_price,item_quantity)
VALUES('$this->user_uid','$this->item_id','$this->item_name','$this->item_price','$this->item_quantity')";
$conn->exec($sql);
$this->last_id=$conn->lastInsertId();
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
public function display_item($uid){
$db = new db();
$conn=$db->conn();
$this->user_uid=$uid;
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$conn->query($sql);
while($row=$statement->fetch()){
$this->iid[]=$row['iid'];
$this->display_item[]=[$row['item_id'],$row['item_name'],$row['item_price'],$row['item_quantity']];
}
}
catch(PDOException $e){
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
}
as you can see from item.php , i have to call
$db = new db();
$conn=$db->conn();
in add_item() and display_item() .
So that means every method that I have to access to database connection, i have to do that. Is there a easier way to do that by not creating $db = new db() in other class by modifying my codes?
OR
Did i miss out some functions that PHP can do that?
My desired way of doing it
require "../../includes/db.php";
$db = new db();
$conn=$db->conn();
class item{
...
...
public function add_item($uid,$item_id,$item_n,$item_p,$item_q){
try {
$sql = "INSERT item(uid,item_id,item_name,item_price,item_quantity)
VALUES('$this->user_uid','$this->item_id','$this->item_name','$this->item_price','$this->item_quantity')";
$conn->exec($sql);
}
public function display_item($uid){
....
try{
$sql="SELECT * FROM item where uid='$this->user_uid'";
$statement=$conn->query($sql);
so that i only declare 1 time()
$db = new db();
$conn=$db->conn();
in class item() and use it for the rest of methods.
Upvotes: 1
Views: 98
Reputation: 41885
Just add the connection in the constructor. Add the necessary property as well:
class db
{
protected $db_host;
protected $db_name;
protected $db_user_name;
protected $db_pass;
protected $db_conn; // add me here
Then inside the constructor, use the credentials there to connect. So that when you create the object, its already connected, you'll just use the property connection for your executing queries and stuff:
public function __construct()
{
$this->db_host = "localhost";
$this->db_name = "bs";
$this->db_user_name = "root";
$this->db_pass = "";
// connect
try {
$this->db_conn = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", $this->db_user_name="root", $this->db_pass);
$this->db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db_conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
exit;
}
}
Then, extend the db
class in item. You can now use db
's db_conn
property inside item
class.
class item extends db
{
public $user_uid;
public $last_id;
public $display_item;
public $iid;
protected $item_id;
protected $item_name;
protected $item_price;
protected $item_quantity;
public function display_item($uid)
{
try {
$stmt = $this->db_conn->prepare('SELECT * FROM item WHERE uid = :uid');
$stmt->bindValue(':uid', $uid);
$stmt->execute();
foreach($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$this->iid[] = $row['iid'];
$this->display_item[] = [$row['item_id'], $row['item_name'], $row['item_price'], $row['item_quantity']];
}
} catch(PDOException $e) {
echo $e->getMessage();
exit;
}
}
}
Sidenote: I've added prepared statements on top
Upvotes: 2