Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3722036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:45:39+00:00 2026-05-19T05:45:39+00:00

I tried to truncate a table but why is it not working? must something

  • 0

I tried to truncate a table but why is it not working? must something wrong in the database query?

$sql = "TRUNCATE TABLE `table_name`";

$result = $connection -> query($sql);

Ideally, I want to truncate all tables in one go – is it possible?

if you wonder what is inside the class that I use to make the database queries, here is it,

#connects the database and handling the result
class __database {

 protected $connection = null;
 protected $error = null;

 #make a connection
 public function __construct($hostname,$username,$password,$database)
 {
  $this -> connection = new mysqli($hostname,$username,$password,$database);

  if (mysqli_connect_errno()) 
  {
   printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
  }
 }

 ...

 #performs a query on the database
 public function query($query)
 {
  $result = $this -> connection -> query($query); 
  if($result) 
  {
   return $result;
  } 
  else
  {
   $this -> error = $this -> connection -> error;
   return false;
  }

 }


 #display error
 public function get_error() 
 {
  return $this -> error;
 }

 #closes the database connection when object is destroyed.
    public function __destruct()
    {
        $this -> connection -> close();
    }
}

thanks.

edit:

below is how I call the db object,

# the host used to access DB
define('DB_HOST', 'localhost');

# the username used to access DB
define('DB_USER', 'root');

# the password for the username
define('DB_PASS', 'xxx');

# the name of your databse 
define('DB_NAME', 'xxx'); 

$connection = new __database(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T05:45:41+00:00Added an answer on May 19, 2026 at 5:45 am

    thanks for the help guys! here is my answer,

    # truncate data from all table
    # $sql = "SHOW TABLES IN 1hundred_2011";
    # or,
    $sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE '".DB_NAME."'";
    
    # use the instantiated db connection object from the init.php, to process the query
    $tables = $connection -> fetch_all($sql);
    //print_r($tables);
    
    foreach($tables as $table) 
    {
        //echo $table['TABLE_NAME'].'<br/>';
    
        # truncate data from this table
        # $sql = "TRUNCATE TABLE `developer_configurations_cms`";
    
        # use the instantiated db connection object from the init.php, to process the query
        # $result = $connection -> query($sql);
    
        # truncate data from this table
        $sql = "TRUNCATE TABLE `".$table['TABLE_NAME']."`";
    
        # use the instantiated db connection object from the init.php, to process the query
        $result = $connection -> query($sql);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To empty a database table, I use this SQL Query: TRUNCATE TABLE `books` How
I need to clear many tables (preferably truncate table). But tables have many FK
I need to clear all my inventory tables. I've tried this: SELECT 'TRUNCATE TABLE
What is the recommended way to truncate a table using hibernate/hql? I've tried this:
Working on postgres SQL. I have a table with a column that contains values
This is the SQL: TRUNCATE TABLE `dc_path`; INSERT INTO dc_path (coords) VALUES('(40.64406436923055, -8.638539251709062)'); INSERT
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.