我目前正在将我的网站转换为 PDO,但遇到了一点问题,我有一个函数可以将日期时间转换为文本,说明它是多久前发布的,但是在将我的查询更改为 PDO 后,该函数向我抛出了一个错误:
错误:
Notice: Undefined variable: retval in C:\wamp\www\pdoconvert\threads.php on line 301 (line 20 of the function)
功能:
function time_ago($date,$granularity=2) {
$date = strtotime($date);
$difference = time() - $date;
$periods = array('decade' => 315360000,
'year' => 31536000,
'month' => 2628000,
'week' => 604800,
'day' => 86400,
'hour' => 3600,
'minute' => 60,
'second' => 1);
if ($difference < 5) { // less than 5 seconds ago, let's say "just now"
$retval = "Posted just now";
return $retval;
} else {
foreach ($periods as $key => $value) {
if ($difference >= $value) {
$time = floor($difference/$value);
$difference %= $value;
$retval .= ($retval ? ' ' : '').$time.' '; <-- Line 301
$retval .= (($time > 1) ? $key.'s' : $key);
$granularity--;
}
if ($granularity == '0') { break; }
}
return ' Posted: '.$retval.' ago';
}
}
询问:
$nostmt = $db->prepare ("SELECT threads.Thread_ID, threads.Thread_Title, threads.Board_ID, threads.Author, threads.Thread_Sticky, threads.Thread_Locked, Sub1.LatestPost, Sub1.PostCount, members.Post_As, members2.Member_ID AS LastPostMemberID, members2.Post_As AS LastPostMemberPostAs
FROM threads
INNER JOIN (SELECT Thread_ID, MAX(posts.Post_DateTime) AS LatestPost, COUNT(*) AS PostCount FROM posts GROUP BY Thread_ID) Sub1
ON threads.Thread_ID = Sub1.Thread_ID
INNER JOIN members
ON threads.Author = members.Member_ID
INNER JOIN posts posts2
ON posts2.Thread_ID = Sub1.Thread_ID AND posts2.Post_DateTime = Sub1.LatestPost
INNER JOIN members members2
ON members2.Member_ID = posts2.Member_ID
WHERE threads.Board_ID=:Board_ID
AND threads.Thread_Sticky='0'
ORDER BY Sub1.LatestPost DESC
LIMIT $offset, $rowsperpage");
$nostmt->bindValue( ":Board_ID", $Board_ID);
$nostmt->execute();
while($row = $nostmt->fetch(PDO::FETCH_ASSOC)) {
$Thread_ID = $row['Thread_ID'];
$Thread_Title = $row['Thread_Title'];
$Board_ID = $row['Board_ID'];
$Author = $row['Author'];
$Thread_Sticky = $row['Thread_Sticky'];
$Thread_Locked = $row['Thread_Locked'];
$Post_Author = $row['Post_As'];
$Posts = $row['PostCount'];
$Post_DateTime = time_ago($row['LatestPost']); <--time-ago function called
$Member_ID = $row['LastPostMemberID'];
$Post_As = $row['LastPostMemberPostAs'];
真正让我感到困惑的是,我在每个输出的行中两次收到此错误,但我也获得了正确的时间前输出。
我应该如何解决这个问题?是我的劣质 PDO 转换还是功能故障?