好的,我有很多零散的拼图。我正在尝试做一个应用程序,更新我的网站底部的报价每月(即‘引用的月份“)。我使用PHP访问MySQL服务器,MySQL服务器中有引号。
在数据库中,表名为“引号”,并包含“引号”、“引号”、“作者”和“datetime_used”列。“datetime_used”列的默认值为1991年,我使用它来检查我是否在去年使用过该引用。如果随机选择的报价时间小于当前时间减去1年,则该报价将用作本月的新报价。如果使用引号,“datetime_used”将更新到当前时间。
我认为要执行的步骤是: 1.运行运行以下PHP脚本的cron作业(每个月将运行):
<?php
//Make connection
$connect = mysqli_connect("localhost","user","password","dbname");
//Check connection
if (mysqli_connect_errno($connect))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Run query
//Select random quote
$result = mysqli_query($connect,
"SELECT quotation, author FROM quotes
WHERE datetime_used <= NOW() - INTERVAL 1 YEAR
ORDER BY RAND()
LIMIT 1");
$row = $mysqli_fetch_array($result);
$quote = $row['quotation'];
//Update the datetime_used column to NOW()
mysqli_query($connect,"
UPDATE quotes SET datetime_used=NOW()
WHERE quotation='$quote'");
mysqli_close($connect);
?><footer>
<p>Quote of the month:
<?php
//INCLUDE THE $quote SOMEHOW
echo $quote;
?>
</p>
</footer>因此,我的问题是如何使cron作业以一种方式更新$quote变量,以便将更新后的变量包含到我的网站代码中。还是有更好的方法来做这件事?
编辑:我想出的代码似乎很有效。
<?php
//Make connection
$connect = mysqli_connect("localhost","root","root","test");
//Check connection
if (mysqli_connect_errno($connect)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Check for month difference between this month and most recently used quote as well as get the quote and author to store
$check = mysqli_query($connect, 'SELECT MONTH(NOW())-MONTH(datetime_used) AS m, quotation, author FROM quotes ORDER BY datetime_used DESC LIMIT 1');
$checkarray = mysqli_fetch_array($check);
$quote = $checkarray['quotation'];
$author = $checkarray['author'];
//If the month difference if 1 or greater, it selects a new quote
if ($checkarray['m']) {
$result = mysqli_query($connect,
"SELECT quotation, author FROM quotes
WHERE datetime_used <= NOW() - INTERVAL 1 YEAR
ORDER BY RAND()
LIMIT 1");
$row = mysqli_fetch_array($result);
$quote = $row['quotation'];
$author = $row['author'];
//Update the datetime_used column to NOW()
mysqli_query($connect,
"UPDATE quotes SET datetime_used=NOW()
WHERE quotation='$quote' AND author='$author'");
}
mysqli_close($connect);
echo "\"$quote\"
</cite>
-$author";
?>发布于 2014-02-01 05:00:04
我觉得你把所需的步骤复杂化了。你只需要:
不需要克伦的工作。我假设这是一个低流量的站点,所以在每个页面访问上运行检查并不是一个性能问题。如果确实需要,可以使用缓存系统缓存所选引号,并避免在30天后检查数据库。
https://stackoverflow.com/questions/21494117
复制相似问题