Jump to content

how do i delete a full row in mysql ?


jd2007

Recommended Posts

Well it depends on the structure of the table you're trying to delete from, but the general syntax is:

 

DELETE FROM <tablename> WHERE <field> = <value>;

 

For example:

 

This is my table structure....

COLOR

--------

ID

Name

Description

 

This is some sample data

1, Red, A red color

2, Green, A green color

 

DELETE FROM `COLOR` WHERE `ID` = 2;

 

No more row 2.

 

I suggest always deleting from tables by using the primary key... when possible. I'd suggest against saying: DELETE FROM `COLOR` WHERE `Name` = 'GREEN';

Link to comment
Share on other sites

If you're just wanting the numbers to be in order.... consider this.

 

$result = mysql_query("SELECT `ID`, `Name` FROM `COLOR`");

$rows = mysql_num_rows($result);

 

echo "<table><tr>";

echo "<th>Fake ID</th>";

echo "<th>Real ID</th>";

echo "<th>Name</th>";

echo "</tr>";

 

for($i = 0; $i < $rows; ++$i )

{

  $row = mysql_fetch_row($result);

  echo "<tr>";

  echo "<td>" . $i . "</td>";

  echo "<td>" . $row[0] . "</td>";

  echo "<td>" . $row[1] . "</td>";

  echo "</tr>";

}

echo "</table>";

 

So then you get a "fake id" that is ordered how you want (i think). But behind the scenes you have the real ids that aren't being updated... and the user doesn't have to see them (and probably shouldnt).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.