PHP : fonctions perso pour DB Mysql

// ——————————————————————– Variables DATABASE ——————————————————————–

define(“SOURCES”, array(
“smtreport” => array(
“host” => “serveurQuiContientMaDB1”,
“user” => “monUserDB”,
“pwd” => monPasswordDeDB,
“schema” => “nomDeSchema1”
),
“exploit” => array(
“host” => “serveurQuiContientMaDB2”,
“user” => “monUserDB”,
“pwd” => monPasswordDeDB,
“schema” => “nomDeSchema2”
)
));

 

// ——————————————————————– DATABASE ——————————————————————–

function getMysqlConnector($schema) {
// Create connection
$conn = new mysqli(SOURCES[$schema][“host”], SOURCES[$schema][“user”], SOURCES[$schema][“pwd”], SOURCES[$schema][“schema”]);

// Check connection
if ($conn->connect_error) {
return NULL;
} else {
return $conn;
}
}

function getSingleValueFromDB($schema, $sql) {
$conn = getMysqlConnector($schema);

$result = mysqli_query($conn, $sql);
return mysqli_fetch_array($result)[0];
}

function getArrayFromDB($schema, $sql) {
$conn = getMysqlConnector($schema);

$result = mysqli_query($conn, $sql);

$return = array();
while ($row = mysqli_fetch_assoc($result)) {
array_push($return, $row);
}
return $return;
}

function sqlRequest($schema, $sql) {
$conn = getMysqlConnector($schema);

$result = mysqli_query($conn, $sql);
return $result;
}

 

//Exemples d’utilsations 

//récuperation de données dans un array

$sql=”SELECT * FROM t_data ” ;
$result=getArrayFromDB(‘nomDeSchema1’, $sql);

foreach($result as $row)
{

print_r($row);

}

 

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *