Php-mysql Course

The DELETE statement is used to delete records from a database table.

  - Syntax:
DELETE FROM table_name WHERE condition
The WHERE condition clause is very important, it specifies which row or rows that should be deleted. It's important becouse once you have deleted a record, there is no way of retrieving it, unless you backed up the database beforehand.
If you leave WHERE out, MySQL will delete every record in a table, making it empty again.
The DELETE statement is sent to the MySQL server with the query() method of the mysqli object.

  - Example
In the previous lessons was created a table named "users" and we added some data in it. Here is how it looks the first three records.
idnamepasswordemailreg_date
1 Marius faith name@domain.net 2011-03-24 09:51:46
2 MarPlo peace new_mail@domain.com 2011-03-27 10:20:58
3 I_AM love address@domain.net 2011-03-24 10:10:27

In this example, we'll delete all the records in the "users" table where name='MarPlo':
<?php
// connect to the "tests" database
$conn = new mysqli('localhost', 'root', 'pass', 'tests');

// check connection
if (mysqli_connect_errno()) {
  exit('Connect failed: '. mysqli_connect_error());
}

// DELETE sql query
$sql = "DELETE FROM `users` WHERE `name`='MarPlo'";

// perform the query and check for errors
if (!$conn->query($sql)) {
  echo 'Error: '. $conn->error;
}

$conn->close();
?>
- This code we'll delete all the rows from the "users" table where name='MarPlo', so, if there are more users (records) with the name MarPlo in this table, MySQL will delete all of them.
To be shure which record will be deleted, you can add another condition in the WHERE clause (with the AND operator), that specifies more exactly which row to be removed, also, you can apply the LIMIT option to set how many rows to be deleted.
Example:
                $sql = "DELETE FROM `users` WHERE `name`='MarPlo' AND `id`=2 LIMIT 1";

After the deletion, the first three rows in "users" table will look like this:
idnamepasswordemailreg_date
1 Marius faith name@domain.net 2011-03-24 09:51:46
3 I_AM love address@domain.net 2011-03-24 10:10:27
4 PloMar love_light a_name@domain.net 2011-03-24 14:39:49
- As you can see, the row with name='MarPlo' (id=2) was deleted.
- If you perform an delete query that doesn't actually remove any record (becouse the WHERE condition doesn't match any row), you won't see any errors but no rows will be affected.

• To delete the table itself, use DROP TABLE:
                    DROP TABLE tablename

• To delete an entire database, including every table therein and all of its data, use DROP DATABASE:
                    DROP DATABASE database_name

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP MySQL - DELETE

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)