-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.php
More file actions
112 lines (88 loc) · 2.42 KB
/
Copy pathexample.php
File metadata and controls
112 lines (88 loc) · 2.42 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
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
<?php
include 'run_query.php';
/*
====================================================
EJEMPLO DE USO DE run_query()
====================================================
La función run_query() permite ejecutar consultas SQL
y obtener los resultados en distintos formatos,
según la opción indicada.
Parámetros:
run_query($query, $option, $debug)
$query : Consulta SQL a ejecutar
$option : Tipo de retorno (1, 2 o 3)
$debug : 0 = normal | 1 = modo depuración
*/
$sql = "SELECT id, text, option FROM mytable";
/*==================================================
OPTION 1 - RETORNO EN JSON (ideal para APIs / AJAX)
==================================================*/
$rw = run_query($sql, 1, 0);
echo $rw;
/*
Salida ejemplo:
{
"data": [
{"text":"Gr"},
{"text":"Kgrs"},
{"text":"Mg"},
{"text":"Tn"}
]
}
*/
/*==================================================
OPTION 2 - ARRAY (LANZA ERROR SI NO HAY REGISTROS)
==================================================*/
$rw = run_query($sql, 2, 0);
echo "<pre>";
print_r($rw);
echo "</pre>";
/*
Salida ejemplo:
Array
(
[0] => Array ( [text] => Gr )
[1] => Array ( [text] => Kgrs )
[2] => Array ( [text] => Mg )
[3] => Array ( [text] => Tn )
)
*/
/*==================================================
OPTION 3 - ARRAY SEGURO (CON O SIN REGISTROS)
==================================================*/
$rw = run_query($sql, 3, 0);
echo "<pre>";
print_r($rw);
echo "</pre>";
/*
Salida ejemplo:
Array
(
[0] => Array ( [text] => Gr )
[1] => Array ( [text] => Kgrs )
[2] => Array ( [text] => Mg )
[3] => Array ( [text] => Tn )
)
*/
/*==================================================
TRABAJANDO CON EL ARRAY DE RESULTADOS
==================================================*/
$rw = run_query($sql, 3, 0);
/* Validar si existen registros */
if (count($rw) >= 1) {
/* Recorrer resultados con foreach */
foreach ($rw as $id => $datos) {
echo $datos['id'];
echo $datos['text'];
echo $datos['option'];
}
/* Acceso directo a una fila y columna */
echo $rw[2]['text'];
}
/*
RECOMENDACIÓN:
- OPTION 1 : APIs / JSON / AJAX
- OPTION 2 : Cuando los datos son obligatorios
- OPTION 3 : Uso general (más seguro y recomendado)
*/
?>