【问题标题】:Why is my variable undefined in TCPDF? [closed]为什么我的变量在 TCPDF 中未定义? [关闭]
【发布时间】:2017-07-19 15:45:22
【问题描述】:

我的 Sql 表

+------------+---------+
|    name    |  price  |
+------------+---------+
|     A      |    70   |
+------------+---------+
|     B      |    70   |
+------------+---------+

我用 TCPDF 创建一个 pdf:

$pdo = $db->prepare("SELECT * FROM table");  
    $pdo->execute(); 
    while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {  
        $result += $row['price'];       
} 

$html = '
<table><tr><th>'.$result.'</th></tr></table>'
;

我希望结果是140,但我收到一条错误消息:

Notice: Undefined variable: result 
TCPDF ERROR: Some data has already been output, can't send PDF file

注意:如果我删除 + 符号。 pdf 创建没有错误,我得到了结果70

【问题讨论】:

标签: php mysql foreach tcpdf calculation


【解决方案1】:

它按照它在锡上所说的做:$result 在您第一次循环数据时未定义。您不能向其中添加任何内容,因为它尚未定义。这应该可以。

$pdo = $db->prepare("SELECT * FROM table");  
    $pdo->execute(); 
    $result = 0.0; // Add this row. I'm assuming 'price' is a decimal
    while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {  
        $result += $row['price'];       
    }

【讨论】:

  • 是的,确实有效!
【解决方案2】:

上线

$result += $row['price'];

这是你做的

$result = $result + $row['price'];

但是第一次运行脚本时,$result 变量没有定义。 添加

$result = 0;

在 $pdo 连接之前或在 while 之前,您应该不会再有任何错误。

【讨论】:

    猜你喜欢
    • 2016-12-03
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    相关资源
    最近更新 更多