gpt4 book ai didi

php - php 脚本中每 24 小时执行一次不同的 sql 查询

转载 作者:行者123 更新时间:2023-11-29 08:07:52 24 4
gpt4 key购买 nike

我有这个 php 脚本,可以在 html 网页中显示 sql 表。这是代码:

 <html>
<head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"><title>Your Page Title</title></head>
<body>
<?php
$database="Batabase_xxx";
mysql_connect ("localhost", "User_xxx", "password_xxx");
mysql_query("SET NAMES 'utf8'");
@mysql_select_db($database) or die( "Unable to select database");
$result = mysql_query( "SELECT id,name,address,phone,hours FROM table_xxx" )
or die("SELECT Error: ".mysql_error());
$num_rows = mysql_num_rows($result);
print "found $num_rows results.<P>";
print "<table width=250 border=1>\n";
while ($get_info = mysql_fetch_row($result)){
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=2/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
?>
</body>
</html>

我的问题是我现在有 23 个不同的 table ,我想每天选择一张不同的 table 。例如:今天我想选择table_1,明天table_2等。当我到达table_23(最后一个表)时,我想重置并从table_1重新开始。这可能吗?(我想避免将 23 个表合并为 1 个)请注意,我对此完全陌生,感谢您的每一次帮助。提前致谢。

最佳答案

我会将其存储在配置值的数据库表中,因为它很容易设置,并且您以后可以轻松地为其他事情添加更多设置。

这是一个例子。首先创建保存配置值的表:

CREATE TABLE config_values (
id int(9) NOT NULL AUTO_INCREMENT,
key_name varchar(50) DEFAULT NULL,
value varchar(200) DEFAULT NULL,
PRIMARY KEY (id)
);

然后为此插入一个条目:

INSERT INTO config_values(key_name, value) VALUES ('day_counter', 1);

然后在执行脚本之前,您会看到今天是星期几,并在表名称中使用 in

$current_day_query = mysql_query(
"SELECT value FROM config_values WHERE key_name = 'day_counter'"
);
$current_day_result = mysql_fetch_row($current_day_query);
$current_day = reset($current_day_result);
$table_name = 'table_' . $current_day;

// code
$result = mysql_query( "SELECT id,name,address,phone,hours FROM $table_name" )
// more code

最后,不要忘记在完成之前增加/重置计数器:

if ($current_day == 23) {
$next_day = 0;
}
else {
$next_day = $current_day + 1;
}
$increment_query = mysql_query(
"UPDATE config_values SET value = $next_day WHERE key_name = 'day_counter'"
);

关于php - php 脚本中每 24 小时执行一次不同的 sql 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22351369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com