0

我目前正在将我的网站转换为 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 转换还是功能故障?

4

2 回答 2

1

您在$retval没有先初始化的情况下使用它,这就是您收到通知的原因。尝试在函数顶部初始化它(例如,初始化为空字符串)。您还可以删除第 301 行上的连接点以达到相同的效果。

PHP 中的通知不是错误。它们旨在警告您潜在的有害结构,如果您没有故意编写它们,以后可能会导致错误。不过,可以在 php.ini 中禁用通知,并且默认情况下是这样。php.ini 中的行如下所示:

error_reporting = E_ALL & ~E_NOTICE
于 2013-07-09T09:07:26.197 回答
1

你的$retval变量在那个时候没有定义(line 301)。

要解决这个问题:

  • $retval在函数的第一行定义变量$retval="";

或者

  • 从行中删除连接301 $retval=($retval ? ' ' : '').$time.' ';

我建议使用第一个选项

于 2013-07-09T09:10:18.257 回答