I have created a function in PHP that checks if a record is existing in the database or not. The query I am using to get the row is:
SELECT COUNT(*) AS Total, Conversation_ID FROM conversation WHERE User_ID = 3 AND With_User_ID = 4
Now, when I run this query manually into my sql tab of phpmyadmin, I get this:
The image shows that there is one row in the database that matches the conditions of my query which is perfectly fine.
Now, here's my PHP function:
public static function CheckIfConversationExists($userid, $withuserid) {
if(Utilities::IsValid($withuserid) && Utilities::IsValid($userid)) {
Database::OpenConnection();
$userId = Utilities::SafeString(Database::$databaseConnection, $userid);
$withUserId = Utilities::SafeString(Database::$databaseConnection, $withuserid);
$query = Database::$databaseConnection->prepare("SELECT COUNT(*) AS Total, Conversation_ID FROM conversation WHERE User_ID = ? AND With_User_ID = ?");
$query->bind_param("ii", $userid, $withuserid);
$result = $query->execute();
$query->bind_result($total, $conversationid);
if($result && $query->num_rows > 0) {
$id = array("Conversation_ID"=>"{$conversationid}");
return json_encode($id);
} else {
return -1;
}
Database::CloseConnection();
}
}
The problem is that when I echo $query->num_rows
the result I get is 0
and because of this my function is returning -1 all the time. However, as you can see that the same query that I run in the database gives me one row.
Now to clear you, you would ask that if the values in the function are correct. Yes they are, I've checked them thrice by echoing them on every possible place I could and the result I got was the same which is 3 and 4
.
Can you please help me?