Yes, there's the wpdb class. Some examples can be found for e.g. here.
Then there's the global $wpdb object that contains results and other parts, set based on the current routing.
You can also utilize the WP_Query class which is more common. There's a default global $wp_query object and the $wp_the_query(?) intermediary object, that holds the default ...
Now I have learned that the way I change the database is not safe regarding SQL injection.
So I wonder where/how did you learn that?
And other than that you should check if those two POST variables are actually set, your toggle_status() code looks fine to me, and $wpdb->update() is an easy way to update a record in the database in WordPress, so you ...
So if you want to get the posts that were commented within the last xx hours, e.g. 12 hours, then try one of the following:
Note though, I'm selecting only published posts (post_status = 'publish') and of the post type (post_type = 'post').
And just replace the value of $hours with your preferred number of hours.
$hours = 12;