1
|
|
|
<?php |
2
|
|
|
/* Database utility functions */ |
3
|
|
|
namespace AL\Db; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Get an error context message |
7
|
|
|
* |
8
|
|
|
* @param $context Human friendly context message |
|
|
|
|
9
|
|
|
* @return An error context string |
|
|
|
|
10
|
|
|
*/ |
11
|
|
|
function get_error_context($context) { |
12
|
|
|
return "(Context: $context)"; |
|
|
|
|
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Prepare a statement for the given query and parameters and handle errors |
17
|
|
|
* |
18
|
|
|
* @param $context A human-friendly string giving the context the query is running |
|
|
|
|
19
|
|
|
* in. This will be output in error messages if something goes wrong |
20
|
|
|
* @param $mysqli Database connection |
|
|
|
|
21
|
|
|
* @param $query The SQL query to execute, using ? placeholders for parameters |
22
|
|
|
* @param $bind_string The string representing the types of parameters to bind |
|
|
|
|
23
|
|
|
* (See mysqli_bind_param()), e.g. "ss" for 2 strings. Pass NULL if the query |
24
|
|
|
* doesn't have parameters |
25
|
|
|
* @param $params The list of parameters to bind. Pass NULL if the query doesn't |
26
|
|
|
* have parameters |
27
|
|
|
* @return A prepared statement that has been executed |
28
|
|
|
*/ |
29
|
|
|
function execute_query($context, $mysqli, $query, $bind_string, ...$params) { |
30
|
|
|
$err_ctx = get_error_context($context); |
31
|
|
|
|
32
|
|
|
$stmt = $mysqli->prepare($query) |
33
|
|
|
or die("Error preparing query [$query] $err_ctx: ".$mysqli->error); |
|
|
|
|
34
|
|
|
|
35
|
|
|
if ($bind_string !== null) { |
36
|
|
|
$stmt->bind_param($bind_string, ...$params) |
37
|
|
|
or die("Error binding parameters $err_ctx"); |
|
|
|
|
38
|
|
|
} elseif (strstr($query, "?")) { |
39
|
|
|
die("Error: The query [$query] contained parameters (?) but didn't have a bind string $err_ctx"); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$stmt->execute() |
43
|
|
|
or die("Error executing statement for [$query] $err_ctx: ".$mysqli->error); |
|
|
|
|
44
|
|
|
|
45
|
|
|
return $stmt; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Bind variables to a prepared statement and handle errors |
50
|
|
|
* |
51
|
|
|
* @param $context A human-friendly string giving the context the query is running |
52
|
|
|
* in. This will be output in error messages if something goes wrong |
53
|
|
|
* @param $stmt Prepared statement to bind results on |
|
|
|
|
54
|
|
|
* @param $params Variables to bind to the statement result |
|
|
|
|
55
|
|
|
*/ |
56
|
|
|
function bind_result($context, $stmt, &...$params) { |
57
|
|
|
$err_ctx = get_error_context($context); |
58
|
|
|
|
59
|
|
|
$stmt->bind_result(...$params) |
60
|
|
|
or die("Error binding results $err_ctx"); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Assemble multiple constraints into a SQL string with the proper |
65
|
|
|
* WHERE / AND syntax depending on the number of constraints |
66
|
|
|
* @param $constraints A list of constraints, like "id = ?" or |
67
|
|
|
* "name LIKE ?". |
68
|
|
|
* @return String A SQL string with constraints suitable |
69
|
|
|
* to use in a SQL query |
70
|
|
|
*/ |
71
|
|
|
function assemble_constraints($constraints) { |
72
|
|
|
$query = ""; |
73
|
|
|
if (count($constraints) > 1) { |
74
|
|
|
$query .= " WHERE ".array_shift($constraints); |
75
|
|
|
$query .= " AND ".join($constraints, " AND "); |
|
|
|
|
76
|
|
|
} elseif (count($constraints) == 1) { |
77
|
|
|
$query .= " WHERE ".array_shift($constraints); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $query; |
81
|
|
|
} |
82
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths