为什么我的变量在TCPDF中没有定义?
16lz
2021-01-22
My Sql Table
我的Sql表
+------------+---------+
| name | price |
+------------+---------+
| A | 70 |
+------------+---------+
| B | 70 |
+------------+---------+
I create a pdf with TCPDF:
我用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>'
;
I expect the result to be 140
, but I get an error message:
我希望结果是140,但是我得到了一个错误信息:
Notice: Undefined variable: result
TCPDF ERROR: Some data has already been output, can't send PDF file
Note: If I remove the +
sign. The pdf is created without errors and I get the result 70
.
注意:如果我删除+号。pdf是在没有错误的情况下创建的,我得到的结果是70。
2 个解决方案
#1
2
It does what it says on the tin: $result
is not defined the first time you loop through your data. You can't add anything to it, because it's not defined yet. This should work.
它执行它在tin上的操作:$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'];
}
更多相关文章
- mysql主从同步报slave_sql_running:no的解决方案
- win7下Django的MySql安装,问题解决方案
- mysql 事物没提交导致事物一直运行解决方案
- 无法连接远程MySQL数据库的解决方案
- [征集] MySQL交叉表解决方案及散分
- PHP OOP和MySQLi连接=致命错误:调用未定义的方法sqmyli::arrayQu
- MySQL 自定义函数.txt
- PHP MYSQL 出现中文乱码的解决方案
- Amoeba for MySQL---分布式数据库Proxy解决方案