PHP code example of bmorais / database

1. Go to this page and download the library: Download bmorais/database library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bmorais / database example snippets


const CONFIG_DATA_LAYER = [
    "driver" => "mysql",
    "host" => "localhost",
    "port" => "3306",
    "dbname" => "database",
    "username" => "user",
    "passwd" => "",
    "options" => [
        PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8, lc_time_names = 'pt_BR'",
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
        PDO::MYSQL_ATTR_FOUND_ROWS => true,
        PDO::ATTR_STRINGIFY_FETCHES => true
    ],
    "homologation" => "homologacao",
    "directory_models" => "App\\Models\\",
    "display_errors_details" => true,
];



class Usuario extends Crud
{
    public function __construct()
    {
        $this->database = "nomeBD";
        $this->tableName = "USUARIO";
        $this->classModel = "AutUserModel";
    }

    /**
    * @param $codusuario
    * @return array|null
    */
    public function buscarIdObj($codusuario)
    {
        $result = $this->select("*","WHERE CODUSUARIO=?",[$codusuario]);
        if ($result){
            return $result;
        } else {
            return null;
        }
    }

    /**
    * @param $codusuario
    * @return AutUserModel[] | null
     */
    public function buscarIdModelExample($codusuario)
    {
        $result = $this->select("*","WHERE CODUSUARIO=?",[$codusuario], true);
        if ($result){
            return $result;
        } else {
            return null;
        }
    }

    /**
    * @param $codusuario
    * @return AutUserModel[] | null
    */
    public function buscarIdModelExample2($codusuario)
    {
        $sql = "SELECT * FROM AUT_USER AS U WHERE U.CODUSUARIO=?";
        $params = array($codusuario);
        $result = $this->executeSQL($sql,$params);
        if (!empty($result)){
            return $this->getObjModel($result,$this->classModel);
        } else {
            return null;
        }
    }
    
    /**
    * @param $coduser
    * @return bool
     */
    public function updateUser($name, $email, $coduser)
    {
        $result = $this->update("NAME, EMAIL", array($name, $email), "CODUSER=?");
        if ($result){
            return true;
        } else {
            return false;
        }
    }
    
    /**
    * @param $name
    * @return bool
    */
    public function insertUser($name, $email)
    {
        $result = $this->insert("NOME, EMAIL", array($name, $email));
        if ($result){
            return true;
        } else {
            return false;
        }
    }
    
    
}