簡體   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