mysqli_query_params.php
Gesponsorde koppelingen
PHP script bestanden
Code (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
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
<?php
/**
* Hulpfunctie voor uitvoeren prepared statements
* @note als pg_query_params() maar dan met array
* @param resource $connection
* @param string $query
* @param array $params
* @return array
*/
function mysqli_query_params($connection, string $query, array $params) : array
{
if (FALSE === $mysqli_stmt = mysqli_prepare($connection, $query)) {return FALSE;}
$format = '';
foreach ($params as $i => $v) {
if (is_string($v)) {$format .= 's';}
elseif (is_int($v)) {$format .= 'i';}
elseif (is_float($v)) {$format .= 'd';}
elseif (is_null($v)) {$format .= 's';}
elseif (is_bool($v)) {$format .= 'i';$params[$i] = boolval($v);}
else {return FALSE;}
}
$args = [$format];
foreach (array_keys($params) as $i) {$args[] =& $params[$i];}
if (FALSE === @call_user_func_array([$stmt, 'bind_param'], $args)) {return FALSE;}
if (FALSE === $stmt->execute()) {return FALSE;}
$res = $stmt->get_result();
$stmt->close();
$array = [];
if (FALSE !== $res and 0 < $oResultaat->num_rows) {
while ($row = $res->fetch_assoc()) {$array[] = $row;}
$res->close();
}
return $array;
}
/**
* Hulpfunctie voor uitvoeren prepared statements
* @note als pg_query_params() maar dan met array
* @param resource $connection
* @param string $query
* @param array $params
* @return array
*/
function mysqli_query_params($connection, string $query, array $params) : array
{
if (FALSE === $mysqli_stmt = mysqli_prepare($connection, $query)) {return FALSE;}
$format = '';
foreach ($params as $i => $v) {
if (is_string($v)) {$format .= 's';}
elseif (is_int($v)) {$format .= 'i';}
elseif (is_float($v)) {$format .= 'd';}
elseif (is_null($v)) {$format .= 's';}
elseif (is_bool($v)) {$format .= 'i';$params[$i] = boolval($v);}
else {return FALSE;}
}
$args = [$format];
foreach (array_keys($params) as $i) {$args[] =& $params[$i];}
if (FALSE === @call_user_func_array([$stmt, 'bind_param'], $args)) {return FALSE;}
if (FALSE === $stmt->execute()) {return FALSE;}
$res = $stmt->get_result();
$stmt->close();
$array = [];
if (FALSE !== $res and 0 < $oResultaat->num_rows) {
while ($row = $res->fetch_assoc()) {$array[] = $row;}
$res->close();
}
return $array;
}