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'];       
    }

更多相关文章

  1. mysql主从同步报slave_sql_running:no的解决方案
  2. win7下Django的MySql安装,问题解决方案
  3. mysql 事物没提交导致事物一直运行解决方案
  4. 无法连接远程MySQL数据库的解决方案
  5. [征集] MySQL交叉表解决方案及散分
  6. PHP OOP和MySQLi连接=致命错误:调用未定义的方法sqmyli::arrayQu
  7. MySQL 自定义函数.txt
  8. PHP MYSQL 出现中文乱码的解决方案
  9. Amoeba for MySQL---分布式数据库Proxy解决方案

随机推荐

  1. 在android的Browser中设置User Agent
  2. Android系列教程之四:Android项目的目录结
  3. Android JNI环境搭建及开发入门
  4. Android activity四种启动模式及Flag
  5. 【Android】Android 代码判断当前设备是
  6. Android 布局 LinearLayout与RelativeLay
  7. 关于Android学习要问的一些问题
  8. Android中popWindow弹出菜单的编写
  9. Android(java)学习笔记106:Android XML解析
  10. android中的命名空间