Jérôme Le Naou
SGBD
data 1
data 2
data 3
.....
Fichiers plats
MySQL / MariaDB
PostgreSQL
Oracle
Serveur
Base
Table
mysql.serveur.com
Base MesCinemas
Salles
Films
Horaires
Utilisateurs
Billets
Colonnes
Lignes
SELECT * FROM Billets WHERE utilisateur_id=5
INSERT INTO Utilisateurs (Login,Email,Password) VALUES
("Jérôme", "j.lenaou@gmail.com", "pgdp2017")
UPDATE Horaires SET Date="01-03-2017", Heure="09:00:00" WHERE ID=5
DELETE FROM Films WHERE ID=2
INSERT INTO <table> (<colonnes>)
VALUES (<expressions>)
DELETE FROM <table>
WHERE <condition>
/!\ Ne JAMAIS oublier le WHERE /!\
SELECT <champs> FROM <table>
[WHERE <condition>]
[ORDER BY <expressions> [ASC|DESC]]
[LIMIT lignes]
UPDATE <table>
SET <champ>=<expression> [,<champ2>=<exp2>, ...]
WHERE <condition>
/!\ Ne JAMAIS oublier le WHERE /!\
SELECT Niveau + 1 FROM Joueurs WHERE ID=5
SELECT MIN(Niveau) as NiveauMin FROM JoueurLevels WHERE Experience > 1000
SELECT MAX(Niveau) as NiveauMax FROM JoueurLevels
SELECT (Nom + ' ' + Prenom) as NomComplet FROM Utilisateurs
SELECT CONCAT(Nom, ' ', Prenom) as NomComplet FROM Utilisateurs
SELECT LENGTH(Prenom) FROM Utilisateurs
SELECT * FROM Utilisateurs WHERE Prenom LIKE 'John'
SELECT * FROM Utilisateurs WHERE Prenom LIKE 'J%'
SELECT * FROM Utilisateurs WHERE Prenom LIKE '%n'
SELECT * FROM Utilisateurs WHERE Prenom LIKE '%h%'
SELECT LOWER(Prenom) FROM Utilisateurs
SELECT UPPER(Prenom) FROM Utilisateurs
SELECT SUBSTR(Prenom, 4, 2) FROM Utilisateurs
SELECT REVERSE(Prenom) FROM Utilisateurs
SELECT REPLACE(Prenom, 'John', 'Marc') FROM Utilisateurs
SELECT TRIM(' Bob ') FROM Utilisateurs => Bob
SELECT TRIM(BOTH 'x' FROM 'xxxBobxxx') FROM Utilisateurs => Bob
SELECT TRIM(LEADING 'x' FROM 'xxxBobxxx') FROM Utilisateurs => Bobxxx
SELECT TRIM(TRAILING 'x' FROM 'xxxBobxxx') FROM Utilisateurs => xxxBob
SELECT *, ADDDATE(Anniversaire, 10) FROM Utilisateurs
SELECT *, ADDDATE(Anniversaire, INTERVAL 1 YEAR) FROM Utilisateurs
SELECT *, SUBDATE(Anniversaire, INTERVAL 1 MONTH) FROM Utilisateurs
SELECT *, ADDTIME(Anniversaire, '1:0:0') FROM Utilisateurs
SELECT *, SUBTIME(Anniversaire, '2:0:0') FROM Utilisateurs
SELECT * FROM Utilisateurs WHERE Anniversaire < SUBDATE('02-01-2017 10:00:00', INTERVAL 20 YEAR)
SELECT *, DATE_FORMAT(Anniversaire, '%W %d %M %Y') FROM Utilisateurs
SELECT * FROM Utilisateurs WHERE YEAR(Anniversaire) > 1987
SELECT COUNT(*), YEAR(Anniversaire) FROM Utilisateurs GROUP BY YEAR(Anniversaire)
Exemple de syntaxe pour les DATETIME :
// index.php
<?php
$CONFIG = require("config.php");
try {
$dbh = new PDO($config["dbType"] . ":host=".$config["host"].";dbname=".$config["dbname"],
$config["user"],
$config["password"]
);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8");
// some queries
}
catch (PDOException $e) {
error_log('PDO Exception: '.$e->getMessage());
die('PDO says no.');
}
// config.php
<?php
return [
"host" => "localhost",
"dbname" => "PGDP_Cinemas",
"user" => "root",
"password" => "root",
"dbType" => "mysql"
];
<?php
// .....
$queryString = "SELECT * FROM Utilsateurs";
$request = $db->query($queryString);
$retour = $req->fetchAll();
foreach($retour as $data) {
// Do some stuff
}
<?php
// .....
$queryString = "UPDATE Utilisateurs SET Login=:login";
$request = $db->prepare($queryString);
$request->execute([
"login" => "john"
]);
if($request->rowCount() == 0) {
die("user not found. No update");
}
else {
// Do some stuff
}
<?php
// .....
$offset = 5;
$queryString = "SELECT * FROM Utilisateurs LIMIT 0, :limit";
$requete = $db->prepare($queryString);
$requete->execute([
"limit" => $offset
]);
/*
!!! ERREUR !!!
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''5'' at line 1
*/
$queryString = "SELECT * FROM Utilisateurs LIMIT 0, 5";
$requete = $db->prepare($queryString);
$requete->execute();
// OK
<?php
// .....
$offset = 5;
$queryString = "SELECT * FROM Utilisateurs LIMIT 0, :limit";
$requete = $db->prepare($queryString);
$requete->bindValue("limit", 5, PDO::PARAM_INT);
$requete->execute();
// OK