Skip to content

Instantly share code, notes, and snippets.

@Komche
Last active August 2, 2019 14:00
Show Gist options
  • Save Komche/941a9d55a81697d0479c222cff7c7854 to your computer and use it in GitHub Desktop.
Save Komche/941a9d55a81697d0479c222cff7c7854 to your computer and use it in GitHub Desktop.
<?php
//fonction pour ce connecter à une base de donnée en utilisant la methode PDO
function db()
{
$db = 'nom_de_la_base_de_donne';
$user_ = 'nom_d_utilisateur';
$pass = 'mot_de_passe';
$host = 'localhost';
try {
$pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user_, $pass, $pdo_options);
} catch (Exception $e) {
die('Erreur :' . $e->getMessage());
}
return $conn;
}
//fonction pour afficher tout les donnée d'une table
function affiche_donnee()
{
$sql = "SELECT * FROM nom_de_la_table";
$req = db->query($sql);
if ($result = $req->fetchAll()) {
return $result;
}
}
//fonction pour afficher un seule enregistrment d'une table
function affiche($id)
{
$sql = "SELECT * FROM nom_de_la_table WHERE id_de_la_table=:id";
$req = db()->prepare($sql);
$req->execute(array('id' => $id));
if ($result = $req->fetch()) {
return $result;
}
}
// fonnction pour ajouter dans une table en PHP
function ajout_dans_une_table($valeur_attribut_1, $valeur_attribut_2, $valeur_attribut_3)
{
$sql = "INSERT INTO nom_de_la_table(attribut_1, attribut_2, attribut_3) VALUE(:attribut_1, :attribut_2, :attribut_3)";
$req = db()->prepare($sql);
$req->execute(array(
'attribut_1' => $valeur_attribut_1,
'attribut_2' => $valeur_attribut_2,
'attribut_3' => $valeur_attribut_3
));
return 1;
}
// fonction pour modifer une table en PHP
function modifier_une_table($valeur_attribut_1, $valeur_attribut_2, $valeur_attribut_3, $id){
$sql = "UPDATE nom_de_la_table SET attribut_1=:attribut_1, attribut_2=:attribut_2, attribut_3=:attribut_3
WHERE id_de_ma_table=:id";
$req = db()->prepare($sql);
$req->execute(array(
'attribut_1' => $valeur_attribut_1,
'attribut_2' => $valeur_attribut_2,
'attribut_3' => $valeur_attribut_3
'id'=>$id
));
return 1;
}
//fonction pour supprimer un enregistrement
public function deleteFile($id)
{
$sql = "DELETE FROM nom_de_la_table WHERE id_de_ma_table=$id";
db()->exec($sql);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment