Truncate all records from whole database tables with a single query using php.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "cms";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
if (!mysql_connect('localhost', 'root', '')) {
    echo 'Could not connect to mysql';
    exit;
}
$sql = "SHOW TABLES FROM $dbname";
$dbresult = mysql_query($sql);
while ($row = mysql_fetch_row($dbresult)) {
foreach($row as $r){
$sql = "truncate ".$r;
echo ' table ====>'.$r.'<br>';
$result = $conn->query($sql);

 }}
?>

Share this

Related Posts

Latest
Previous
Next Post »