Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
277 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
5 | * Copyright 2019 Daniel Marschall, ViaThinkSoft |
||
6 | * |
||
7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
8 | * you may not use this file except in compliance with the License. |
||
9 | * You may obtain a copy of the License at |
||
10 | * |
||
11 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
12 | * |
||
13 | * Unless required by applicable law or agreed to in writing, software |
||
14 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
16 | * See the License for the specific language governing permissions and |
||
17 | * limitations under the License. |
||
18 | */ |
||
19 | |||
20 | class OIDplusQueryResultMySQLNoNativeDriver extends OIDplusQueryResult { |
||
21 | // Based on https://www.php.net/manual/de/mysqli-stmt.get-result.php#113398 |
||
22 | |||
23 | protected $stmt; |
||
24 | protected $nCols; |
||
25 | protected $no_resultset; |
||
26 | |||
27 | public function __construct($stmt) { |
||
28 | $metadata = mysqli_stmt_result_metadata($stmt); |
||
29 | |||
30 | $this->no_resultset = is_bool($metadata); |
||
31 | |||
32 | if (!$this->no_resultset) { |
||
33 | $this->nCols = mysqli_num_fields($metadata); |
||
34 | $this->stmt = $stmt; |
||
35 | |||
36 | mysqli_free_result($metadata); |
||
37 | } |
||
38 | } |
||
39 | |||
40 | public function containsResultSet(): bool { |
||
41 | return !$this->no_resultset; |
||
42 | } |
||
43 | |||
44 | public function num_rows(): int { |
||
45 | if ($this->no_resultset) throw new OIDplusException("The query has returned no result set (i.e. it was not a SELECT query)"); |
||
46 | |||
47 | $this->stmt->store_result(); |
||
48 | return $this->stmt->num_rows; |
||
49 | } |
||
50 | |||
51 | public function fetch_array()/*: ?array*/ { |
||
52 | if ($this->no_resultset) throw new OIDplusException("The query has returned no result set (i.e. it was not a SELECT query)"); |
||
53 | |||
54 | // https://stackoverflow.com/questions/10752815/mysqli-get-result-alternative , modified |
||
55 | $stmt = $this->stmt; |
||
56 | $stmt->store_result(); |
||
57 | $resultkeys = array(); |
||
58 | $thisName = ""; |
||
59 | |||
60 | if ($stmt->num_rows==0) return null; |
||
61 | |||
62 | for ($i = 0; $i < $stmt->num_rows; $i++) { |
||
63 | $metadata = $stmt->result_metadata(); |
||
64 | while ($field = $metadata->fetch_field()) { |
||
65 | $thisName = $field->name; |
||
66 | $resultkeys[] = $thisName; |
||
67 | } |
||
68 | } |
||
69 | |||
70 | $ret = array(); |
||
71 | $args = array(); |
||
72 | for ($i=0; $i<$this->nCols; $i++) { |
||
73 | $ret[$i] = NULL; |
||
74 | $theValue = $resultkeys[$i]; |
||
75 | $ret[$theValue] = NULL; // will be overwritten by mysqli_stmt_bind_result |
||
76 | $args[] = &$ret[$theValue]; |
||
77 | } |
||
78 | if (!mysqli_stmt_bind_result($this->stmt, ...$args)) { |
||
79 | return null; |
||
80 | } |
||
81 | |||
82 | // This should advance the "$stmt" cursor. |
||
83 | if (!mysqli_stmt_fetch($this->stmt)) { |
||
84 | return null; |
||
85 | } |
||
86 | |||
87 | // Return the array we built. |
||
88 | return $ret; |
||
89 | } |
||
90 | |||
91 | public function fetch_object()/*: ?object*/ { |
||
92 | if ($this->no_resultset) throw new OIDplusConfigInitializationException("The query has returned no result set (i.e. it was not a SELECT query)"); |
||
93 | |||
94 | $ary = $this->fetch_array(); |
||
95 | if (!$ary) return null; |
||
96 | |||
97 | $obj = new stdClass; |
||
98 | foreach ($ary as $name => $val) { |
||
99 | $obj->$name = $val; |
||
100 | } |
||
101 | return $obj; |
||
102 | } |
||
103 | } |