ADD : création de la première requête GET pour les users
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
parent
dc50875216
commit
8ca5766a11
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/*
|
||||
$db_dsn="mysql:dbname=bobParty;host=127.0.0.1;port=8889";
|
||||
//$db_host = '127.0.0.1';
|
||||
$db_user = 'root';
|
||||
$db_password = 'root';
|
||||
//$db_db = 'bobParty';
|
||||
//$db_port = 8889;
|
||||
|
||||
$mysqli = new mysqli(
|
||||
$db_host,
|
||||
$db_user,
|
||||
$db_password,
|
||||
$db_db,
|
||||
$db_port
|
||||
);
|
||||
|
||||
if ($mysqli->connect_error) {
|
||||
echo 'Errno: '.$mysqli->connect_errno;
|
||||
echo '<br>';
|
||||
echo 'Error: '.$mysqli->connect_error;
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
echo '<br>';
|
||||
echo 'Host information: '.$mysqli->host_info;
|
||||
echo '<br>';
|
||||
echo 'Protocol version: '.$mysqli->protocol_version;
|
||||
|
||||
$mysqli->close();
|
||||
|
||||
try{
|
||||
$dbh = new PDO($db_dsn,$db_user,$db_password);
|
||||
$dbh->exec("set names utf8");
|
||||
echo 'Success: A proper connection to MySQL was made.';
|
||||
}catch(PDOException $exception){
|
||||
echo "Connection error : " . $exception->getMessage();
|
||||
}
|
||||
*/
|
||||
class Database{
|
||||
public $connection;
|
||||
|
||||
public function establishConnection(){
|
||||
$this->connection=null;
|
||||
|
||||
try{
|
||||
$this->connection = new PDO("mysql:dbname=bobParty;host=127.0.0.1;port=8889", "root", "root");
|
||||
$this->connection->exec("set names utf8");
|
||||
}catch(PDOException $exception){
|
||||
echo "Connection error : " . $exception->getMessage();
|
||||
}
|
||||
|
||||
return $this->connection;
|
||||
}
|
||||
}
|
||||
?>
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
class User{
|
||||
private $connection;
|
||||
private $table= "User";
|
||||
|
||||
// object properties
|
||||
public $id;
|
||||
public $username;
|
||||
public $password;
|
||||
public $nationality;
|
||||
public $sex;
|
||||
public $dateOfBirth;
|
||||
public $currentBobCoins;
|
||||
public $totalBobCoins;
|
||||
public $nbGamesPlayed;
|
||||
|
||||
public function __construct($db){
|
||||
$this->connection=$db;
|
||||
}
|
||||
|
||||
public function read(){
|
||||
$sqlQuery= "SELECT U.id, U.username, U.password, U.nationality, U.sex, U.dateOfBirth, U.currentBobCoins, U.totalBobCoins, U.nbGamesPlayed FROM User U";
|
||||
$query = $this->connection->prepare($sqlQuery);
|
||||
$query->execute();
|
||||
return $query;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
//Headers
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Content-Type: application.json; charset=UTF-8");
|
||||
header("Access-Control-Allow-Method: GET");
|
||||
header("Access-Control-Max-Age: 3600");
|
||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||
|
||||
// Checking that the method is correct and dealing with the error
|
||||
if($_SERVER['REQUEST_METHOD'] != 'GET'){
|
||||
http_response_code(405);
|
||||
echo json_encode(["message" => "Unauthorized method"]);
|
||||
}else{
|
||||
include_once '../dbConnection.php';
|
||||
include_once '../models/User.php';
|
||||
|
||||
$db= new Database();
|
||||
$db= $db->establishConnection();
|
||||
|
||||
$user = new User($db);
|
||||
|
||||
$stmt = $user->read();
|
||||
|
||||
if($stmt->rowCount() >= 0){
|
||||
|
||||
$arrayUser=[];
|
||||
$arrayUser['users']=[];
|
||||
|
||||
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
|
||||
extract($row);
|
||||
|
||||
$user= [
|
||||
"id" => $id,
|
||||
"username" => $username,
|
||||
"password" => $password,
|
||||
"nationality" => $nationality,
|
||||
"sex" => $sex,
|
||||
"dateofBirth" => $dateOfBirth,
|
||||
"currentBobCoins"=>$currentBobCoins,
|
||||
"totalBobCoins" => $totalBobCoins,
|
||||
"nbGamesPlayed" => $nbGamesPlayed,
|
||||
];
|
||||
|
||||
$arrayUser['users'][]=$user;
|
||||
}
|
||||
|
||||
http_response_code(200);
|
||||
|
||||
echo json_encode($arrayUser);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in new issue