gpt4 book ai didi

php - 如何使用 php 和 mysql 按 id 顺序引用当天的报价

转载 作者:行者123 更新时间:2023-11-29 00:54:47 26 4
gpt4 key购买 nike

我正在尝试连接 MySQL 数据库并获取数据我需要它按 ID(每日)顺序获取(数据)并将两个数据分开并通过两个不同的变量调用它们数据库看起来像这样:

------------------------------
| ID | Data | Data2
------------------------------
| 1 | tea | hot
| 2 | milk | hot
| 3 | pepsi | cold

并且输出将只有一行(一个数据)

我没有像上面那样做 :) 它只是为了澄清....以及 UTF-8 中大数据的最佳排序和类型是什么?

编辑:
你能让它返回 5 行而不是 1 行吗?按身份证?例如第一天 (1-2-3-4-5) ID 和第二天 (6-7-8-9-10) 等等?

最佳答案

    quotes
----------------------------------
| id | data | data2
----------------------------------
| 1 | first quote | translated quote
| 2 | second... | bla bla

然后你选择它:

   $firstday="2011-06-06";
$getquote = mysql_query("SELECT * FROM quotes WHERE id=(DATEDIFF(CURDATE()+1, '$firstday'))");
$quote = mysql_fetch_object($getquote);
echo $quote->data . $quote->data2;

编辑!!:我已经消除了 datediff,因此从日期差异返回的 ID 直接在 WHERE 中。

它的作用是计算第一天和当前日期之间的差异。所以每天 datediff 都会变大 1。DATEDIFF(CURDATE()+1, '$firstday') as datediff 可以解释为

datediff = differenceBetween(Currentday +1 and firstDay)
  • 昨天是 2011-07-06,因此 datediff = 2011-07-07(有 +1!)- 2011-07-061
  • 今天是 2011-07-08 - 2011-07-062
  • 明天 2011-07-09 - 2011-07-063
  • 后天 2011-07-10 - 2011-07-064
  • 一个月后将是 2011-08-08 - 2011-07-0633

因此,datediff 每天增加 1

quotes
-------------------------
|id| data
-------------------------
|1| quote day 1 (because date difference from start == 1)
|2| quote 2 day 2 (datediff == 2)
|3| quote 3 day 3 (datediff == 3)
|4| quote 4 day 4
.....

或者很快:每天都会有不同的报价,从 ID 1 开始。

我无法解释更多......


编辑 #2:每天 5 个引语

$offset = date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d');
$getquote = "SELECT * FROM quotes LIMIT {$offset},5";

第二次编辑感谢 ajreal ( SQL LIMIT syntax error )


编辑 #3:每天 5 个引号,随变量变化..

选项 1:

$choose=0; //statically defined, only first of that day will pop out

选项 2:

$choose = mysql_real_escape_string($_GET["qid"]); //which one will be defined in url.. (watch out, people can figure it out and browse through all quotes

选项 3:

$choose = rand(0,4); //will choose it randomly from those 5 daily quotes

因此,选择您喜欢的选项之一,并在此之前添加:

$offset = 5*date_diff(new DateTime('now'), new DateTime('2011-08-29'))->format('%d') + $choose;
$getquote = mysql_query("SELECT * FROM quotes WHERE id = '$offset'");
$quote = mysql_fetch_object($getquote);
echo $quote->data . $quote->data2;

关于php - 如何使用 php 和 mysql 按 id 顺序引用当天的报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6601117/

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