I am creating a MySQLi database class for my own personal use to clean up the prepared statements usage within functions on my own code and for my own learning and experience. However I have run into a complication when running 2 or more queries on a single page.
Using my basic SELECT function I can run 2 queries as such:
$data = $db->SELECT("SELECT * FROM hist WHERE id = ?", array('i', 2));
$data2 = $db->SELECT("SELECT * FROM hist WHERE id = ?", array('i', 3));
and as seen, these two queries are saved to separate variables.
Doing a var_dump on $data shows me the correct result, for id=2:
array(1) {
[0]=>
array(5) {
["id"]=>
int(2)
["value"]=>
float(51.4)
}
}
However doing a var_dump on $data2 shows me the data for id=2 and id=3 which sort of suggests the results are concatenating with each other?
array(2) {
[0]=>
array(5) {
["id"]=>
int(2)
["value"]=>
float(51.4)
}
[1]=>
array(5) {
["id"]=>
int(3)
["value"]=>
float(476)
}
}
What could be causing this and how could this be fixed? Do I have to initiate a new instance of the class per query? I tried to unset the resulting data at the end of the function but this didn't seem to help.
My SELECT function from my class:
public function SELECT($sql, $args=null) {
if ($stmt = $this->link->prepare($sql)) {
if(isset($args)) {
$method = new ReflectionMethod('mysqli_stmt', 'bind_param');
$method->invokeArgs($stmt, $this->refValues($args));
}
if(!$stmt->execute()) {
array_push($this->err, 'execute() failed: ' . htmlspecialchars($stmt->error));
}
$result = $stmt->get_result();
if (count($result) >= 1) {
while($row = $result->fetch_array(MYSQLI_ASSOC)) {
array_push($this->rs, $row);
}
}
else {
array_push($this->rs, "No data has been returned.");
}
}
else {
array_push($this->err, 'prepare() failed: ' . htmlspecialchars($this->link->error));
}
if(!empty($this->err)) {
if($this->debug) {
return $this->err;
}
}
else {
return $this->rs;
}
$stmt->close();
unset($this->err);
unset($this->rs);
}
$dataand$data2supposed to contain? What does your table contains?$datai get 1 result, forid=2as expected. If ivar_dumpon$data2i get 2 results, 1 forid=2and 1 forid=3which is not expected. It seems to be 'adding' the two results together query after query. I am sure the database is correct, especially since my ID is a pk and of course can only return 1 result if its a pk.refValues()). Conclusion: it's not easy to help... :)$method = new ReflectionMethod('mysqli_stmt', 'bind_param');? Why not just call$stmt->bind_param()?