Echo success on delete mysql data
I have a MySQL table. My PHP script is suppose to return true
if a row get deleted. But, I am getting true
, no matter whether the row get deleted or not. I am posting the code below;
$query = "delete from myt开发者_JAVA技巧able where qwerty='asdf'";
$delete = mysql_query($query);
if($delete){
echo "true";
}else{
echo "false";
}
I want to echo true
if row get deleted and false
if not get deleted. How can I do this?
Straight From PHP Manual, you should use use mysql_affected_rows
:
mysql_query('delete from mytable where qwerty='asdf'');
printf("Records deleted: %d\n", mysql_affected_rows());
That way, you can check out how many records were affected by your command. Adjust accordingly.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. Use mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
http://php.net/manual/en/function.mysql-query.php
http://www.php.net/manual/en/function.mysql-affected-rows.php
You can use the mysql_affected_rows
function to find out if any rows were actually deleted.
http://www.php.net/manual/en/function.mysql-affected-rows.php
精彩评论