繁体   English   中英

php脚本每24小时不同的sql查询

[英]different sql query every 24 hours in php script

我有这个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_1,tommorow 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);

然后在执行脚本之前,您会看到它的当天,并在表名中使用

$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'"
);
<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
if(isset($_COOKIE['daynumber']) && $_COOKIE['daynumber']!=NULL)
{
if($_COOKIE['daynumber']!=23) { $_COOKIE['daynumber'] += 1; }
}
else
{
  $_COOKIE['daynumber'] = 1;
}
$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_".$_COOKIE['daynumber'])
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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM