-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.php
131 lines (112 loc) · 4.16 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
// LocalHost
//$mysqli = new mysqli("localhost", "root", "", "db");
//http://darkvier.site40.net/
$mysqli = new mysqli("localhost", "id1230894_darkvier", "dark1234", "id1230894_test");
// Comprobar la conexión
if ($mysqli->connect_errno) {
printf("false\nFalló la conexión: %s\n", $mysqli->connect_error);
exit();
}
// Si queremos que nos de el ranking
if($_GET['accion'] === "listar"){
$sql = "SELECT Nickname, Puntuacion, DATE_FORMAT(Fecha, '%d/%m/%Y') AS Fecha
FROM Ranking WHERE Dificultad = 'Facil'
ORDER By Puntuacion DESC LIMIT 10";
if ($resultado = $mysqli->query($sql, MYSQLI_USE_RESULT)) {
echo "true".PHP_EOL;
while($fila = $resultado->fetch_assoc()){
echo $fila['Nickname']."\t".$fila['Puntuacion']."\t".$fila['Fecha']."\n";
}
$resultado->close();
}else{
echo "false".PHP_EOL;
echo "Error al listar el ranking". PHP_EOL;
echo "Error: " . $sql . "<br>" . PHP_EOL . $mysqli->error . PHP_EOL;
}
echo "\t\t\n";
$sql = "SELECT Nickname, Puntuacion, DATE_FORMAT(Fecha, '%d/%m/%Y') AS Fecha
FROM Ranking WHERE Dificultad = 'Normal'
ORDER By Puntuacion DESC LIMIT 10";
if ($resultado = $mysqli->query($sql, MYSQLI_USE_RESULT)) {
while($fila = $resultado->fetch_assoc()){
echo $fila['Nickname']."\t".$fila['Puntuacion']."\t".$fila['Fecha']."\n";
}
$resultado->close();
}
echo "\t\t\n";
$sql = "SELECT Nickname, Puntuacion, DATE_FORMAT(Fecha, '%d/%m/%Y') AS Fecha
FROM Ranking WHERE Dificultad = 'Dificil'
ORDER By Puntuacion DESC LIMIT 10";
if ($resultado = $mysqli->query($sql, MYSQLI_USE_RESULT)) {
while($fila = $resultado->fetch_assoc()){
echo $fila['Nickname']."\t".$fila['Puntuacion']."\t".$fila['Fecha']."\n";
}
$resultado->close();
}
}elseif($_GET['accion'] === "listarRecords" AND $_GET['Nickname'] != ""){
$sql = "SELECT DISTINCT(Dificultad) AS Dif,
(SELECT Puntuacion FROM Ranking
WHERE Nickname = '".$_GET['Nickname']."' AND Dificultad = Dif) AS Punt
FROM Ranking ORDER BY Dif ASC LIMIT 10";
if ($resultado = $mysqli->query($sql, MYSQLI_USE_RESULT)) {
echo "true".PHP_EOL;
while($fila = $resultado->fetch_assoc()){
echo $fila['Dif']."\t".$fila['Punt']."\n";
}
$resultado->close();
}else{
echo "false".PHP_EOL;
echo "Error al listar los recors". PHP_EOL;
echo "Error: " . $sql . "<br>" . PHP_EOL . $mysqli->error . PHP_EOL;
}
}else if($_GET['accion'] === "insertar"){
if ($_GET['Nickname'] != "" AND $_GET['Puntuacion'] > -1 AND $_GET['Dificultad'] != "" ){
// Comprobar si existe una puntuacion
$sql = "SELECT Puntuacion FROM Ranking WHERE Nickname = '".$_GET['Nickname']."'
AND Dificultad = '".$_GET['Dificultad']."'";
if ($result = $mysqli->query($sql)) {
$row_cnt = $result->num_rows;
$fila = $result->fetch_assoc();
// Insertar si no hay puntuacion
if($row_cnt == 0){
$sql = "INSERT INTO Ranking (Nickname, Puntuacion, Fecha, Dificultad)
VALUES ('".$_GET['Nickname']."', '".$_GET['Puntuacion']."',
now(), '".$_GET['Dificultad']."')";
if ($mysqli->query($sql) === TRUE) {
echo "true".PHP_EOL;
} else {
echo "false".PHP_EOL;
echo "Error insertando puntuacion". PHP_EOL;
echo "Error: " . $sql . "<br>" . PHP_EOL . $mysqli->error . PHP_EOL;
}
// Actualizar puntuacion si existe una menor
}elseif($row_cnt == 1 && $fila['Puntuacion'] < $_GET['Puntuacion']){
$sql = "UPDATE Ranking SET Puntuacion = '".$_GET['Puntuacion']."', Fecha = now()
WHERE Nickname = '".$_GET['Nickname']."' AND Dificultad = '".$_GET['Dificultad']."'";
if ($mysqli->query($sql) === TRUE) {
echo "true".PHP_EOL;
} else {
echo "false".PHP_EOL;
echo "Error actualizando puntuacion". PHP_EOL;
echo "Error: " . $sql . "<br>" . PHP_EOL . $mysqli->error . PHP_EOL;
}
}else{
echo "true".PHP_EOL;
echo "Puntuacion no superada". PHP_EOL;
}
}else{
echo "false".PHP_EOL;
echo "Error al comprobar puntuacion anterior". PHP_EOL;
echo "Error: " . $sql . "<br>" . PHP_EOL . $mysqli->error . PHP_EOL;
}
}else{
echo "false".PHP_EOL;
echo "Insercion sin datos". PHP_EOL;
}
}else{
echo "false".PHP_EOL;
echo "Accion no definida". PHP_EOL;
}
$mysqli->close();
?>