使用PHP和MYSQLi在数据库中每24小时删除数据 [英] Deleting Data each 24 hours in Database with PHP and MYSQLi

查看:449
本文介绍了使用PHP和MYSQLi在数据库中每24小时删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道你是如何做的:

I like to know how you do this:

我做了一个聊天框,自动更新自己的脚本,它加载最新的50

I have made a sort of a chatbox that automatic updates it self with a script and it loads up the latest 50 chat messages from database.

现在我想知道,如何在每24小时后删除邮件,但保留第一个新的50条邮件,然后删除较旧的邮件。

Now i wonder, how to delete messages after each 24 hours but keep the first new 50 messages and delete the older ones after that.

这是否可能与MYSQLi和PHP或必须使用更好的解决方案吗?

Is this possible with MYSQLi and PHP or must i do it with a better solution?

UPDATE

UPDATE

Lund的后续建议和我的提供商的快速回复:
我无法执行Cron Jobs或MYSQL与我目前的计划的事件。
我也没有钱升级它,所以erm ....如果有
另一个解决方案,谢谢你帮我解决这个问题。

Followed advise from Lund and a fast response from my provider: I am not able to do Cron Jobs or MYSQL Events with my current plan. Also i do not have the money for upgrading it, so erm.... if there is another solution, thank you for helping me out on this matter folks.

我有一个名为:chatbox
的表,其中有:

I have a table called: chatbox and have this in it:

C_ID
chattext
name
date


推荐答案

伪代码(实际上不会工作没有一些编辑来适应你的代码,但它的一个想法) - 我不知道你的数据,或其变量名。或者你的表名。

Pseudo code (Won't actually work without some edits to fit your code, but its an idea) - I don't know how your data is stored, or its variable names. Or your table name. Or basically any information that I would need to actually get this to work.

$sql = ("SELECT * FROM ChatLog ORDER BY TimeAdded DESC"); //Order by descending should show the newest ones first.

$index = 1; //set an index to loop through

while($row = mysqli_query($con, $sql) { //use a while loop to go through the table

    if($index <= 50) { //for the first 50 records
        //do nothing
    } else { //for everything after 50 records
        $chatId = $row['chatLogID']; //ID or unique value for the actual message.
        $sql = ("DELETE FROM ChatLog WHERE chatLogID='$chatId'"); //Delete it from the table
        mysqli_query($con, $sql);//Execute query.
    }

}


$ b b

您可以将此设置为每次存储消息时执行,或者根据服务器的当前时间检查它是否每天删除。

You can set this up to execute everytime a message is stored, or check it against the current time of the server to remove it daily.

更新:THIS CODE应该使用您的代码

UPDATE: THIS CODE SHOULD BE WORKING WITH YOUR CODE

$sql = ("SELECT * FROM chatbox ORDER BY time DESC"); //Order by descending should show the newest ones first.

$index = 1; //set an index to loop through

while($row = mysqli_query($con, $sql) { //use a while loop to go through the table

    if($index <= 50) { //for the first 50 records
        //do nothing
    } else { //for everything after 50 records
        $chatId = $row['C_ID']; //ID or unique value for the actual message.
        $sql = ("DELETE FROM chatbox WHERE C_ID='$chatId'"); //Delete it from the table
        mysqli_query($con, $sql);//Execute query.
    }

}

这篇关于使用PHP和MYSQLi在数据库中每24小时删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆