How delete all data from database in php?


Delete Data From a MySQL Table Using MySQLi and PDO

The DELETE statement is used to delete records from a table:

DELETE FROM table_name
WHERE some_column = some_value

Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

To learn more about SQL, please visit our SQL tutorial.

Let's look at the "MyGuests" table:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30
3 Julie Dooley 2014-10-26 10:48:23

The following examples delete the record with id=3 in the "MyGuests" table:

Example (MySQLi Object-oriented)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>




Example (MySQLi Procedural)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM MyGuests WHERE id=3";

if (mysqli_query($conn, $sql)) {
  echo "Record deleted successfully";
} else {
  echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


Example (PDO)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // sql to delete a record
  $sql = "DELETE FROM MyGuests WHERE id=3";

  // use exec() because no results are returned
  $conn->exec($sql);
  echo "Record deleted successfully";
} catch(PDOException $e) {
  echo $sql . "
" . $e->getMessage();
}

$conn = null;
?>


After the record is deleted, the table will look like this:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30



I would like to scrap all my records in a database. I want to use just a PHP script, not PhpMyAdmin. I have will be using a MySQL database administrator account. What SQL query should I use?

TRiG

9,8246 gold badges55 silver badges106 bronze badges

asked Apr 26, 2012 at 16:04

3

You could run a query:

/* Assumes the existence of a few key variables
   and further, that your user has the appropriate permissions */

mysql_connect( $host, $user, $pass ) or die( mysql_error() );
mysql_select_db( $db ) or die( mysql_error() );

mysql_query( "TRUNCATE TABLE tablename" );

Or

mysql_query( "DELETE FROM tablename" );

CAUTION!

These queries will result in all records being deleted. If you want only certain records to be dropped, add a where clause:

mysql_query( "DELETE FROM tablename WHERE userid = 5" );

answered Apr 26, 2012 at 16:06

SampsonSampson

261k74 gold badges530 silver badges559 bronze badges

To completely empty the database, dropping all tables (if that's what you really want to do), run this PHP script.

answered Apr 26, 2012 at 16:43

TRiGTRiG

9,8246 gold badges55 silver badges106 bronze badges

How delete multiple data from database in PHP?

Steps in PHP Multiple Rows Update/Delete.
Select rows using checkbox input..
Show form UI for updating table columns..
Submit array of row details to PHP..
Iterate through row details array to apply update/delete query for each..

How do I delete all data in MySQL?

Click the box With selected: and choose Drop. This will execute the DROP TABLE SQL query on all tables and once you confirm that you wish to proceed the database will be emptied.

How do I delete a database in PHP?

Deleting a Database If a database is no longer required then it can be deleted forever. You can use pass an SQL command to mysql_query to delete a database.

How edit or delete data from database in PHP?

php (to display the records from the database), insert. php (to insert records into the database), edit. php (to edit records), and delete. php (to delete records).