I have some mysql_query()s that work just fine, but if I put them in an if else statement they return false. Any idea?
public function delete_post() {
$id = $_GET['post_id'];
mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");
if ( !mysql_query() ) {
echo ('Could not be Deleted');
} else {
echo 'Deleted Successfully';
}
}
and when i run it, it deletes the post, but returns "Could not be deleted"
You're running a query with an empty SQL statement, which is not correct SQL. Try this.
$result = mysql_query("DELETE ...");
if (!$result) {
echo ('Could not be Deleted');
} else {
echo 'Deleted Successfully';
}
The difference is on line 2 (of my code).
Note: mysql_query() is deprecated. You really should use PDO::query instead.
If you still wish to use it, do:
$result = mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");
if (!$result)
{
echo ('Could not be Deleted');
}
Explanation:
In you original code you call mysql_query()
two times. The second time, there is no argument, so it doesn't work, and that's what your code is reporting.
I think this is a pretty similar question / answer: What does a successful MySQL DELETE return? How to check if DELETE was successful?
Can you try what it suggests and see if that will work for you?
You could try :
public function delete_post() {
$id = $_GET['post_id'];
$query = mysql_query("DELETE FROM posts WHERE post_id='$id' LIMIT 1");
if ( !$query ) {
echo ('Could not be Deleted');
} else {
echo 'Deleted Successfully';
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.