Jump to content


This topic is now archived and is closed to further replies.


MYSQL delete loop

Recommended Posts

Hello fellow php scripters,

I am new to this forum, so please be easy on me.
I don't have much experience, but i like to tell myself i do.  ;)
I made a database driven navigation menu, or a folder structure menu... whatever you would like to call it.

it's structure is quit easy.

CREATE TABLE `file_manager` (
  `id` int(11) NOT NULL auto_increment,
  `menu_id` int(11) NOT NULL,
  `menu_name` varchar(128) collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)

So let's say i have 5 menu items and all items have several sub items.. I want to delete a item with sub items, here i get stuck with my loop.

  $result = mysql_query("SELECT * FROM article_menu WHERE id='$is_the_first_item_to_delete'");  
while($row = mysql_fetch_array($result)){

  $result_sub = mysql_query("SELECT * FROM article_menu WHERE id='$row[menu_id]'");  
while($row_sub = mysql_fetch_array($result_sub)){
  mysql_query("DELETE FROM article_menu WHERE menu_id='$row_sub[id]'");
  mysql_query("DELETE FROM article_menu WHERE menu_id='$is_the_first_item_to_delete'");

This will only delete the first sub items, however i would like to have all sub items to be deleted.
I have searched this site but i couldn't find anything useful. This could be caused by my searching skills.

Hopefully one of you scripters could help me out. ???

Share this post

Link to post
Share on other sites
From what you've stated, this should, in theory, should work:

$query = mysql_query("SELECT menu_id FROM article_menu WHERE id='$is_the_first_item_to_delete'") or die ('Aye, I think we have a problem!');
$r = mysql_fetch_array($query) //Results from query
$menu_id = $r['menu_id']; //The menu id

//Delete all items where menu_id is $menu_id
mysql_query("DELETE FROM article_menu WHERE menu_id='$menu_id'");

This would essentially delete all items with the menu_id of x, where x is the value of $menu_id.

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.