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(); ?>
$query = "DELETE FROM `testtbl` WHERE `UserID` = '$myfieldcontent' "; // Remember to execute it! mysql_query($query); PHP: Try that as your $query. Jay
It seems you have missed something out there. Try the following and compare the difference. Hope it will help you.
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
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.
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.
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
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
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).