Do you need to close mysqli statements?

Problem


I am trying to calculate if there is a registration with the same phone number and date. This is to ensure that people do not register two reservations on the same date (time). My problem is that I expect the query to return 0 , because there is no registration with the same data in the database. The result, however, is 1 . This would mean that the database already has something like that, but no. I assume that it is associated with a request that I am executing before the current request. I wonder if I should close the expression.

What i tried


I tried to close the expression $stmt->close() , but to no avail. I tried to create new variables instead of reusing those from the previous query, but that didn't work either. I triple checked the database to make sure there is no registration with the same details, and I am 100% sure that there is nothing wrong with the database.

Code


Here is my code. I think the problem lies in the first part of the request.
 $query = "SELECT count(*) as reservation_amount FROM reservation WHERE `date`= ?"; $stmt = $db->prepare($query); $stmt->bind_param("s", $datetime); $stmt->execute(); $stmt->bind_result($count); while($row = $stmt->fetch()){ $res_count = $row; //res_count is 1 in this case. I'm thinking I need to close something here? } $query = "SELECT count(*) as registered FROM reservation WHERE `number` = ? and `date`= ?"; $stmt = $db->prepare($query); $stmt->bind_param("is", $number, $datetime); $stmt->execute(); $stmt->bind_result($count); while($row = $stmt->fetch()){ $registered = $row; //outputs 1 but I'm expecting 0 } 

Enter


 datetime = 2014-12-28 17:00:00 number = 0612345678 

Database


The database contains the following entry:
 id name surname number email date amount 5 Henk Houtman 9060666 henk@houtman.nl 2014-12-28 17:00:00 1 


No mistakes.
+5
source share
1 answer

mysqli_stmt_fetch will return true on success. If successful, your query will always return one and only one row (because you are querying for COUNT , with one row containing the required value).

 while($row = $stmt->fetch()){ $registered = $count; } 

You must use the associated variable $count when determining the value in the returned string. (Note: you do not need the $row variable.)

+2
source

Source: https://habr.com/ru/post/1210004/


All Articles