开发者

Problem deleting records with one query in 2 tables

When I try this directly in phpMyAdmin:

DELETE FROM n开发者_如何学Goavigation WHERE id='14';DELETE FROM subnavigation WHERE navi_id='14';

It works but when I it doesnt work with my PHP query that I make:

$db->query('DELETE FROM navigation WHERE id='.$id.';DELETE FROM subnavigation WHERE navi_id='.$id.';');

It gives me this error and doesnt delete anything:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELETE FROM subnavigation WHERE navi_id=14' at line 1

I think that I am missing something really small! Thank you for your time and help.


mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

http://php.net/manual/en/function.mysql-query.php

PhpMyAdmin explodes the string based on the delimiter and executes as multiple queries.

If you want to go off the book (not recommended)

You just have to pass flag 65536 as mysql_connect's 5 parameter (client_flags). This value is defined in /usr/include/mysql/mysql_com.h:

#define CLIENT_MULTI_STATEMENTS (1UL << 16) /* Enable/disable multi-stmt support */

UPDATE

Create a trigger on the first table, so ON DELETE it will also delete from the 2nd table too.


Make it one query.

//don't forget to protect yourself from SQL-injection attacks!
$id = mysql_real_escape_string($id);

$delete_query = " DELETE n, sn from navigation n
                  LEFT JOIN subnavigation sn ON (n.id = sn.id)
                  WHERE n.id = '$id' ");
$db->query($delete_query);


Multiple statements like that are not a query, they form a script. Look into submitting SQL scripts. http://www.shinephp.com/php-code-to-execute-mysql-script/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜