I think any application has two very important features: Saving/Adding Data to Database and then fetching retrieving it back.
Rest everything such as front end: HTML and DOM is another detail, including structuring application by using correct and optimized coding practices such as MVC model establishing files and folder + Object Oriented PHP.
Back to the discussion: I was trying to understand PDO I stumbled upon this online free video course → = Section 5 Dealing with the PDO and the Databa
I have shared a link because in the future if anyone comes at this link he may get the reference.
Before writing this post and seeking help I tried to understand as much as I can on my own.
I am copy-pasting the code here of a class name:
Database.php →
<?php
class Database{
private $host = 'localhost';
private $user = 'toolcula_apps';
private $pass = 'XXXXXXXX##';
private $dbname = 'toolcula_apps';
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host='. $this->host . ';dbname='. $this->dbname;
// Set Options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// Create new PDO
try {
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOEception $e){
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if(is_null($type)){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
There is a very huge information given out here →
https://www.php.net/manual/en/book.pdo.php
I have lots of questions I am asking one by one here.
Question #1 →
https://www.php.net/manual/en/pdostatement.bindparam.php
And the code in question →
public function bind($param, $value, $type = null){
if(is_null($type)){
switch(true){
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
Although, I understand the switch statement, but could not understand what is going on here.