Symbianize Forum

Most of our features and services are available only to members, so we encourage you to login or register a new account. Registration is free, fast and simple. You only need to provide a valid email. Being a member you'll gain access to all member forums and features, post a message to ask question or provide answer, and share or find resources related to mobile phones, tablets, computers, game consoles, and multimedia.

All that and more, so what are you waiting for, click the register button and join us now! Ito ang website na ginawa ng pinoy para sa pinoy!

PHP PDO connection to Class (OOP Style)

vespercore02

Proficient
Advanced Member
Messages
219
Reaction score
0
Points
26
Hello mga KaSym,

tanong ko lang pano ko ma cacall pdo connection ko to other class?

ang ginawa ko kasi kada public function may global $dbcon; which is parang nirerepeat ko lang ng nirerepeat kada public function,

nagtry na ako mag search kaso di ko magets ung ilan.. newly learned lng OOP sa PHP ahaha

Thanks in advance sa makakatulong.
 
Try mo iexternal file ung connection mo then iinclude mo nalang.
 
include 'pdoconnection.php';

global $dbh;

$query = "INSERT INTO table_name SET name='hello' WHERE id=1 ";

$insert = $dbh->prepare($query);

if($insert->execute())
{
$lastindex = $dbh->lastInsertId();
return array('error'=> FALSE,'message'=> 'Data successfully inserted.','lastindex'=> $lastindex,'query'=> $query);
}
else
{
return array('error'=> TRUE,'message'=> 'Unable to save data.','lastindex'=> $lastindex,'query'=> $query);
}
 
Last edited:
Hello, Share ko lang yun natutunan ko.

eto yun config file ko. dito ko sinisave yun mga database credentials

Code:
return [
	'database' => [
		'dbhost' => 'mysql:host=localhost',
		'dbname' => 'test',
		'dbuser' => 'root',
		'dbpass' => '',
		'dboptions' => [
			PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
		]
	]
];

eto naman yun connection class ko. dedicated sya para sa pdo connection

Code:
class Connection
{
	public static function start($config)
	{
		try {
			return new PDO(
				$config['dbhost'] . ';dbname=' . $config['dbname'],
				$config['dbuser'],
				$config['dbpass'],
				$config['dboptions']
			);
		} catch (Exception $e) {
			die($e->getMessage());
		}
	}
}

dito naman yun mga query sa database.

Code:
class QueryBuilder
{
	protected $pdo;

	public function __construct($pdo)
	{
		$this->pdo = $pdo;
	}

	public function selectAll($table)
	{
		$statement = $this->pdo->prepare("SELECT * FROM {$table}");
		$statement->execute();

		return $statement->fecth(PDO::FETCH_CLASS);
	}
}

eto yun index file ko.

Code:
$configuration = require 'configuration.php';
require 'connection.php';
require 'querybuilder.php';

$database = new QueryBuilder(
	Connection::start($configuration['database'])
);

yun sa tanong mo naman kung pano ipasa yun pdo sa ibang class. Sakto yun example ko dyan. Gawa ka ng class ng connection tapos ipasa mo sa ibang class via __construct method.
 
Hello, Share ko lang yun natutunan ko.

eto yun config file ko. dito ko sinisave yun mga database credentials

Code:
return [
	'database' => [
		'dbhost' => 'mysql:host=localhost',
		'dbname' => 'test',
		'dbuser' => 'root',
		'dbpass' => '',
		'dboptions' => [
			PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
		]
	]
];

eto naman yun connection class ko. dedicated sya para sa pdo connection

Code:
class Connection
{
	public static function start($config)
	{
		try {
			return new PDO(
				$config['dbhost'] . ';dbname=' . $config['dbname'],
				$config['dbuser'],
				$config['dbpass'],
				$config['dboptions']
			);
		} catch (Exception $e) {
			die($e->getMessage());
		}
	}
}

dito naman yun mga query sa database.

Code:
class QueryBuilder
{
	protected $pdo;

	public function __construct($pdo)
	{
		$this->pdo = $pdo;
	}

	public function selectAll($table)
	{
		$statement = $this->pdo->prepare("SELECT * FROM {$table}");
		$statement->execute();

		return $statement->fecth(PDO::FETCH_CLASS);
	}
}

eto yun index file ko.

Code:
$configuration = require 'configuration.php';
require 'connection.php';
require 'querybuilder.php';

$database = new QueryBuilder(
	Connection::start($configuration['database'])
);

yun sa tanong mo naman kung pano ipasa yun pdo sa ibang class. Sakto yun example ko dyan. Gawa ka ng class ng connection tapos ipasa mo sa ibang class via __construct method.


salamat sir eto nga kelangan ko, madali ko din siyang nagets san m natutunan yan? :D
 
Eto naman config ko
database.php
<?php
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "databasename";
try {
$conn = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user,$db_pass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e->getMessage();
}
include_once('database.php');
$conn = new Controller($conn);
?>

class.connection.php
<?php
class Connection {
private $db;
function __construct($conn){
$this->db = $conn;
}
}
?>
 
Back
Top Bottom