-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.php
More file actions
59 lines (54 loc) · 1.96 KB
/
db.php
File metadata and controls
59 lines (54 loc) · 1.96 KB
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
<?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.
*/
include_once 'config.php';
function db_connect(){
$mysqli = new mysqli(DB_HOSTNAME, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno) {
$response['error'] = true;
$response['msg'] = "Error: Fallo al conectarse a MySQL debido a: \n";
$response['msg'] .= "Errno: " . $mysqli->connect_errno . "\n";
$response['msg'] .= "Error: " . $mysqli->connect_error . "\n";
} else {
$response['error'] = false;
$response['conexion'] = $mysqli;
}
return $response;
}
function db_close($mysqli){
$mysqli->close();
}
function db_query_select($query_select,$mysqli){
if (!$resultado = $mysqli->query($query_select)) {
$response['error'] = true;
$response['msg'] = "La ejecución de la consulta falló debido a: \n";
$response['msg'] .= "Query, " . $query_select . "\n";
$response['msg'] .= "Errno, " . $mysqli->errno . "\n";
$response['msg'] .= "Error, " . $mysqli->error . "\n";
}
if ($resultado->num_rows === 0) {
$response['error'] = true;
$response['msg'] = "cero resultados\n";
} else {
$response['error'] = false;
$response['result'] = $resultado->fetch_assoc();
}
return $response;
}
function db_query_insert($query_insert,$mysqli){
if (!$resultado = $mysqli->query($query_insert)) {
$response['error'] = true;
$response['msg'] = "La ejecución de la consulta falló debido a: \n";
$response['msg'] .= "Query, " . $query_insert . "\n";
$response['msg'] .= "Errno, " . $mysqli->errno . "\n";
$response['msg'] .= "Error, " . $mysqli->error . "\n";
} else {
$response['error'] = false;
$response['respuesta'] = "New record created successfully";
}
return $response;
}
?>