0
Below I will pass an example of how I use today the PDO connection to access my databases. I would like to see with you, if it is a good practice this way, or if it is recommended a safer and more efficient way.
For example:
<?php
require "environment.php";
global $pdo;
$config = array();
$config['options'] = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES
UTF8");
if(ENVIRONMENT == "DEVELOPMENT"){
$config['dbname'] = '';
$config['host'] = '';
$config['user'] = '';
$config['password'] = '';
} ELSE IF(ENVIRONMENT == "PRODUCTION"){
$config['dbname'] = '';
$config['host'] = '';
$config['user'] = '';
$config['password'] = '';
}
try{
$pdo = new PDO("mysql:dbname=".$config['dbname'].";
host=".$config['host'], $config['user'], $config['password'],
$config['options']);
}catch(PDOExcepetion $e){
echo "ERRO: ".$e->getMessage();
exit;
}
Then in class I use it like this:
<?php
class Categorias{
public function getListaCategorias(){
$array = array();
global $pdo;
$sql = $pdo->query("Select * from categorias");
if($sql->rowCount() > 0){
$array = $sql->fetchAll();
}
return $array;
}
}
Thanks Leandro! I will read the articles you have indicated to me.
– Weliton Sernajotto
Please mark it as the correct one if possible and agree to my answer! Thanks @Welitonsernajotto
– Leandro Curioso