MySQL DataBase automatic reset after 24 hour for Demo Mode

ashi567

Member
Sep 23, 2022
31
12
8
I wish to create a demo mode for my CMS and is there anyway with which I can set my MySQL database automatically reset after 24 hour of time.(all my customer data should remove within 24h but without remove my demo data to)
 

ha2000

Member
Trusted Uploader
Jul 7, 2022
93
57
18
You can use cron job to set a job for 24 hours. That job will have to remove all your database tables & recreate with demo content.
 

alexmisaila

Legend member
Trusted Uploader
May 9, 2020
1,140
922
133
⭐⭐⭐⭐⭐
I wish to create a demo mode for my CMS and is there anyway with which I can set my MySQL database automatically reset after 24 hour of time.(all my customer data should remove within 24h but without remove my demo data to)
here is a sample script in PHP that could be used to reset the MySQL database after 24 hours of time using a cron job:
Code:
<?php

// Connect to MySQL server
$db_host = "localhost";
$db_user = "username";
$db_pass = "password";
$db_name = "database_name";

$conn = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

// Delete all customer data from database
$delete_customer_data = "DELETE FROM customers WHERE is_demo = 0";

if (mysqli_query($conn, $delete_customer_data)) {
  echo "Customer data deleted successfully" . PHP_EOL;
} else {
  echo "Error deleting customer data: " . mysqli_error($conn) . PHP_EOL;
}

// Close connection to MySQL server
mysqli_close($conn);

?>
To set up the cron job, you would need to add this script to your server and configure the cron daemon to run it at a specific time every day. For example, to run the script at midnight every day, you could add the following line to the crontab file:

0 0 * * * /path/to/reset_database.php
This is just a sample script and may need to be modified to fit the specific needs of your CMS
 

Forum statistics

Threads
69,206
Messages
908,351
Members
236,895
Latest member
jeremcastdlp

About us

  • Our community has been around for many years and pride ourselves on offering unbiased, critical discussion among people of all different backgrounds. We are working every day to make sure our community is one of the best.

Quick Navigation

User Menu