<?php

/*

powered by Trésor Sukami

Sépia Technologies 

version 1.1

*/

$db_charset='utf8';



$db_dns="mysql:host=$db_host;dbname=$db_name;charset=$db_charset";

$db_options=[

    PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,

    PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC

];

/** Connection */

try{

    $cn=new PDO($db_dns,$db_user,$db_pwd,$db_options);

    $bdd=$cn;

}

catch(exception $e){echo$e->getMessage();exit();}



/** database functions */



function rows($sql){

    global $cn;

    $ret=[];

    try{

        $q=$cn->query($sql);

        while($r=$q->fetch()){

            $ret[]=$r;

        }

    }

    catch(exception $e){}

    return $ret;

}

function row($sql){

    global $cn;

    $ret=[];

    try{

        $q=$cn->query($sql);

        if($r=$q->fetch()){

            $ret=$r;

        }

    }

    catch(exception $e){}

    return $ret;

}

function get($sql,$key='data'){

    global $cn;

    $ret=null;

    try{

        $q=$cn->query($sql);

        if($r=$q->fetch()){

            $ret=$r[$key];

        }

    }

    catch(exception $e){}

    return $ret;

}

function gets($sql,$key='data'){

    global $cn;

    $ret=[];

    try{

        $q=$cn->query($sql);

        while($r=$q->fetch()){

            $ret[]=$r[$key];

        }

    }

    catch(exception $e){}

    return $ret;

}

function execute($sql){

    global $cn;

    $ret='';

    try{

        $q=$cn->exec($sql);

        return $cn->lastInsertId();

    }

    catch(exception $e){}

    return $ret;

}

function prepare($sql,$array=[],$map=[]){

    global $cn;

    $q=$cn->prepare($sql);

    $i=0;

    foreach($array as $row){

        foreach($map as $field){

            $i++;

            $q->bindValue($i,$row[$field]);

        }

        if($i){

            $q->execute();

            $i=0;

        }

    }

    

}