-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.php
182 lines (166 loc) · 5.49 KB
/
mysql.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
define("SERVERNAME", "localhost");
define("USERNAME", "root");
define("PASSWORD", "913004560");
define("DBNAME", "quizLotyv1");
function selectData($sql){
// Create connection
$conn = mysqli_connect(SERVERNAME, USERNAME, PASSWORD, DBNAME);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$resultado[] = $row;
}
} else {
$resultado = 0;
}
mysqli_close($conn);
return $resultado;
}
function insertData($sql){
// Create connection
$conn = mysqli_connect(SERVERNAME, USERNAME, PASSWORD, DBNAME);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$result = mysqli_query($conn, $sql);
$last_id = mysqli_insert_id($conn);
mysqli_close($conn);
return $result;
}
function getUsers(){
$sql = "SELECT * FROM usuarios";
$result = selectData($sql);
return $result;
}
function getQuestionbyQuestID($question_id){
$sql = "SELECT * FROM preguntas where ID=".$question_id;
$result = selectData($sql);
return $result;
}
function getRespuestasbyQuestID($question_id){
$sql = "SELECT * FROM respuestas where id_pregunta=".$question_id;
$result = selectData($sql);
return $result;
}
function getResultadosTOT(){
$sql = "SELECT * FROM usuarios order by puntos desc";
$result = selectData($sql);
return $result;
}
function getRespuestaCorrectabyQuestID($question_id){
$sql = "SELECT * FROM respuestas where correcto=1 and id_pregunta=".$question_id;
$result = selectData($sql);
if ($result==0){
$result = "not_calculada";
}
return $result;
}
function getResultadosbyQuestID($question_id){
$sql = "SELECT usuarios.username,respuestas.letra,respuestas.correcto "
. "FROM usuarios_respuestas,respuestas,usuarios "
. "WHERE usuarios_respuestas.pregunta_id=".$question_id." "
. "AND usuarios_respuestas.respuesta_id=respuestas.ID "
. "AND usuarios_respuestas.user_id=usuarios.ID;";
$result = selectData($sql);
return $result;
}
function createUser($fromName,$fromNumber){
$sql = "INSERT INTO quizLotyv1.usuarios (username, telefono, puntos) VALUES ('".$fromName."', '".$fromNumber."', 0)";
$result = insertData($sql);
return $result;
}
function createResultado ($user_id,$pregunta_id,$respuesta_id){
$sql = "INSERT INTO quizLotyv1.usuarios_respuestas (user_id, pregunta_id, respuesta_id) VALUES (".$user_id.", ".$pregunta_id.", ".$respuesta_id.");";
$result = insertData($sql);
return $result;
}
function userExist($fromNumber){
$resultado = FALSE;
$sql = "SELECT * FROM usuarios where telefono='".$fromNumber."'";
$result = selectData($sql);
if ($result!=0){
$resultado = TRUE;
}
return $resultado;
}
function resultadoExist($user_id,$pregunta_id){
$resultado = FALSE;
$sql = "SELECT * FROM usuarios_respuestas where user_id=".$user_id." and pregunta_id=".$pregunta_id;
$result = selectData($sql);
if ($result!=0){
$resultado = TRUE;
}
return $resultado;
}
function getuserBYphone($fromNumber){
$sql = "SELECT * FROM usuarios where telefono='".$fromNumber."'";
$result = selectData($sql);
return $result;
}
function getRespuestaBYLetra($question_id,$letra){
$sql = "SELECT * FROM respuestas where id_pregunta=".$question_id." and letra='".$letra."'";
$result = selectData($sql);
return $result;
}
function checkResponseProvided ($user_id,$question_id){
$resultado = FALSE;
$sql = "SELECT * FROM usuarios_respuestas where user_id=".$user_id." and pregunta_id=".$question_id;
$result = selectData($sql);
if ($result!=0){
$respuesta_id = $result[0]["respuesta_id"];
$sql = "SELECT * FROM respuestas where ID=".$respuesta_id;
$result = selectData($sql);
if ($result!=0){
if ($result[0]["correcto"]){
$resultado = TRUE;
}
}
}
return $resultado;
}
function insertPoint ($user_id){
$sql = "UPDATE usuarios SET puntos = puntos + 1 WHERE ID=".$user_id;
$result = insertData($sql);
return $result;
}
function getResponsesBYQuestion($question_id){
$sql = "SELECT * FROM usuarios_respuestas where pregunta_id='".$question_id."'";
$result = selectData($sql);
return $result;
}
function checkifCorrectoSet ($question_id){
$resultado = FALSE;
$sql = "SELECT * FROM respuestas where id_pregunta=".$question_id;
$result = selectData($sql);
foreach ($result as $value) {
if ($value['correcto']){
$resultado = TRUE;
break;
}
}
return $resultado;
}
function setCorrectBymayoria($question_id){
$sql = "SELECT respuesta_id FROM usuarios_respuestas "
. "WHERE pregunta_id=".$question_id." GROUP by respuesta_id ORDER by count(respuesta_id) DESC limit 1";
$result = selectData($sql);
if ($result!=0){
$respuesta_id = $result[0]["respuesta_id"];
$sql = "UPDATE respuestas SET correcto = '1' WHERE ID =".$respuesta_id;
$result = insertData($sql);
}
return $result;
}
?>