NYCPHP Meetup

NYPHP.org

[nycphp-talk] MySQL: Delete row

Rick Olson rick at napalmriot.com
Tue Apr 3 23:37:23 EDT 2007


> Scenario:
>
> Query the database table to get the result set:
>
> $Query = "SELECT * FROM $Tablename";
>
> $Result = mysql_query($Query);  //Returns the result set
>
> if (mysql_num_rows($Result) == 1)    //if row I'm looking for is found
>
{
    while ($row = mysql_fetch_array($Result, MYSQL_ASSOC)) {
        $query = "DELETE FROM $Tablename WHERE [primary_key_field[s]] = 
'{$row['primary_key_result']}'";
       mysql_query($query);
    }
}

Since I'm not sure on your database structure, I'm not really able to 
fill in the blanks there... not to mention you don't have a WHERE clause 
on that query, and you're checking to make sure there's only one row; 
unless your table has only one row in it, it's likely nothing will 
happen.  Also, if you're looking to delete them, why not just replace 
your "SELECT * FROM" to a "DELETE FROM ..."?

--
Rick



More information about the talk mailing list