Trying to delete rows in a table with PHP -Please Help!

Discussion in 'MySQL' started by j0563, Jun 2, 2010.

  1. #1
    I'm not that proficient at MySQL so I hope someone can help me-

    I have a database that keeps track of user accounts. I am trying to make it so that all the accounts with the same owner are deleted. This is defined as "owner".

    
    // First gather all users to delete from the table df_users:
    
    $adminusers = mysql_query("SELECT id FROM df_users WHERE owner=$docsuserid"); 
    // I'm trying to locate all the rows whose owner equals the value of $docsuserid and save their id for use below.
    // This only pulls one record- I need to pull all that exist! 
    
    
    // Now that we have all the user id's to delete, we need to delete every row from the table df_users_permissions whose id matches:
    
    mysql_query("DELETE FROM df_users_permissions WHERE id=$adminusers"); 
    
    
    PHP:
    I know it's all wrong. My problems are:

    1. My MySQL query is only pulling one record at a time
    2. I need to get the rows to delete in the table df_users_permissions. (Should I be using some sort of foreach?? )

    ANY help would be MUCH appreciated!!!
     
    j0563, Jun 2, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You just need to loop through your result set.

    
    
    $adminusers = mysql_query("SELECT id FROM df_users WHERE owner=$docsuserid"); 
    // I'm trying to locate all the rows whose owner equals the value of $docsuserid and save their id for use below.
    // This only pulls one record- I need to pull all that exist! 
    
    while($admin = mysql_fetch_array($adminusers)) {
    
    mysql_query("DELETE FROM df_users_permissions WHERE id=".$admin['id']);
    
    }
    PHP:
    If that doesn't work, change your second query to:

    mysql_query("DELETE FROM df_users_permissions WHERE id='".$admin['id']."');
     
    jestep, Jun 2, 2010 IP
    j0563 likes this.
  3. j0563

    j0563 Guest

    Messages:
    153
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    jestep: That's perfect! Thank you for your help! +REP :D
     
    j0563, Jun 2, 2010 IP
  4. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0