DELETE row. How?

Discussion in 'PHP' started by MichaelLewis, Apr 5, 2008.

  1. #1
    1) How do we delete a row from a mysql table?
    2) Where can I find a good on-line reference?

    References I found told me to use the query form:
    "DELETE from mytablename where mykeyfield = $myfieldcontent"
    but this doesn't work.

    To be specific, here is my code:
    <?php
    $username = root;
    $database="testdb";
    $myfieldcontent = "JohnDoe";

    $link = mysql_connect('localhost', $username);
    if (!$link)
    {
    die('Not connected : ' . mysql_error());
    }

    // make foo the current db
    $db_selected = mysql_select_db($database, $link);
    if (!$db_selected)
    {
    die ('Can\'t use ' .$database .':'. mysql_error());
    }

    $query = "Delete FROM testtbl".
    "where UserID = $myfieldcontent";

    mysql_close();
    ?>
     
    MichaelLewis, Apr 5, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    
    $query = "DELETE FROM `testtbl` WHERE `UserID` = '$myfieldcontent' ";
     // Remember to execute it!
    mysql_query($query);
    
    PHP:
    Try that as your $query.

    Jay
     
    jayshah, Apr 5, 2008 IP
  3. srobona

    srobona Active Member

    Messages:
    577
    Likes Received:
    57
    Best Answers:
    0
    Trophy Points:
    88
    #3
    It seems you have missed something out there. Try the following and compare the difference. Hope it will help you.

     
    srobona, Apr 5, 2008 IP
  4. MichaelLewis

    MichaelLewis Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #4
    It still doesn't work!
    Here is my corrected code.

    $query = "DELETE FROM 'testtbl' WHERE 'UserID' = '$myfieldcontent'";
    mysql_query($query);

    Re SROBONA's reply/suggestion that I put in $password, it's not necessary.
    I'm not using a password and connection with the DB works the way I have it.
    I know because of the additional code I have after the delete which is to list my DB rows and it lists them. That's where I can see that the row is not being deleted.

    Here is my listing code for your info:
    $link = mysql_connect('localhost', $username);
    if (!$link) {die('Not connected : ' . mysql_error());}
    $db_selected = mysql_select_db($database, $link);
    if (!$db_selected) {die ('Can\'t use ' .$database .':'. mysql_error());}
    $query = "SELECT UserID FROM testtbl";
    $Results = mysql_query($query, $link);
    while ($row = mysql_fetch_array($Results, MYSQL_ASSOC))
    {print("{$row["UserID"]}<br>"); }
    print("EOJ<br>");
    mysql_close();

    Thanks,
    Michael
     
    MichaelLewis, Apr 5, 2008 IP
    srobona likes this.
  5. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #5
    Try this;

    $query = "DELETE FROM 'testtbl' WHERE 'UserID' = '" . $myfieldcontent . "'";
    mysql_query($query);


    Try printing the query and running it in PHPMYADMIN and see what happens.
     
    Weirfire, Apr 5, 2008 IP
  6. DartPHP

    DartPHP Banned

    Messages:
    71
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    $query = "DELETE FROM `testtbl` WHERE `UserID` = '$myfieldcontent'";
    mysql_query($query);
    
    PHP:
     
    DartPHP, Apr 5, 2008 IP
  7. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'd say the problem now is the single quotes around your table/row name. I know some people said to use them however, they didn't say single quotes. Single quote: ', the one they said to use: ` (it should be the key to the left of your 1/! key). Not that you truly have to place those when doing a query like you are, just saying not to use actual single quotes (') around them. At least I don't think they'd work. Other than that, your query should be fine.
     
    zerxer, Apr 5, 2008 IP
  8. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Hi ,

    Just use single quoto at $myfieldcontent and run the work. let us know what is the result.

    Is it showing any error at the browser screen?

    regards
    Sabbir
     
    Sabbir, Apr 5, 2008 IP
  9. MichaelLewis

    MichaelLewis Active Member

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #9
    There were no error messages.
    I just wasn't getting any results.

    The special quote ` solved the problem.
    Following is my succesful code.

    $query = "DELETE FROM `testtbl` WHERE `UserID` = '$myfieldcontent'";
    mysql_query($query,$link);

    Thank you everyone,
    Michael
     
    MichaelLewis, Apr 9, 2008 IP
  10. singh.ajit05

    singh.ajit05 Peon

    Messages:
    83
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    $query = "DELETE FROM testtbl WHERE UserID = ".$myfieldcontent."";
    mysql_query($query,$link);
     
    singh.ajit05, Apr 10, 2008 IP
  11. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Yeah, you don't need that special quote, I was just saying that that was your problem (thinking the special quote was a regular single quote in someone else's example). You only need single (or double) quotes around your column values (if they're not integers).
     
    zerxer, Apr 11, 2008 IP