kevincro Posted March 21, 2008 Share Posted March 21, 2008 If I have a database table that has three columns. They are named user_name, number, user_id. Lets say that the values in 8 seperate rows are as follows: joe 85 7 bill 85 10 Dave 83 22 Sue 85 3 Andy 85 5 Susan 83 13 Jim 85 9 Kenneth 83 16 My question is, how do I construct a query in PHP/MySQL that will delete only the first entry with the number column value of 83. In this case that would be Dave. Thanks Kevin Link to comment https://forums.phpfreaks.com/topic/97247-using-php-to-delete-rows-in-a-database/ Share on other sites More sharing options...
ohdang888 Posted March 21, 2008 Share Posted March 21, 2008 mysql_query("DELETE FROM `table` WHERE `your_column` = '80' LIMIT 1") or die(mysql_error()); or for a variable, changing query... mysql_query("DELETE FROM `table` WHERE `your_column` = '{$number}' LIMIT 1 ") or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/97247-using-php-to-delete-rows-in-a-database/#findComment-497630 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 unless you use a more details WHERE clause or an ORDER BY clause, you can't be certain that Dave's 83 will be the one deleted. is there a column that is primary key for this table? Link to comment https://forums.phpfreaks.com/topic/97247-using-php-to-delete-rows-in-a-database/#findComment-497644 Share on other sites More sharing options...
ohdang888 Posted March 21, 2008 Share Posted March 21, 2008 ya, hes right. You have more than 1 column under "83", it will delete all of them (if you remove the LIMIT 1) Link to comment https://forums.phpfreaks.com/topic/97247-using-php-to-delete-rows-in-a-database/#findComment-497836 Share on other sites More sharing options...
BlueSkyIS Posted March 21, 2008 Share Posted March 21, 2008 what i meant was that even if you insert one record before another, you can't be sure that the first one you inserted will be the first one deleted. don't think of a (at least MySQL) table as a stack of records. it's more like a random jumble; if you don't specify a record, you never know what you'll get. Link to comment https://forums.phpfreaks.com/topic/97247-using-php-to-delete-rows-in-a-database/#findComment-497841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.