标题:请分析一下我的代码哪里有问题?运行报错呢
只看楼主
jingfang_434
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2009-10-24
结帖率:37.5%
 问题点数:0 回复次数:2 
请分析一下我的代码哪里有问题?运行报错呢
运行后会导出一个xls表格,但是打开表格后里面是错误信息,实在不会排查错误,请大神帮忙看看
Notice: Undefined variable: goods_name in D:\phpStudy\WWW\db.php on line 38            
Notice: Undefined variable: gongjinjia in D:\phpStudy\WWW\db.php on line 39            
Notice: Undefined variable: xinghao in D:\phpStudy\WWW\db.php on line 40            
Notice: Undefined variable: caizhi in D:\phpStudy\WWW\db.php on line 41            


<?php
    error_reporting(E_ALL ^ E_DEPRECATED);
    $localhost = '127.0.0.1';
    $dbname = 'test';
    $dbuser = 'root';
    $dbpwd = 'root';
    $tbname = "yz_info";

    ob_end_clean();
    header('Content-type: text/html; charset=utf-8');
    header("Content-type:application/vnd.ms-excel;charset=UTF-8");
    header("Content-Disposition:filename=data.xls");// 文件名自己改,默认data.xls

    $conn = mysql_connect($localhost,$dbuser,$dbpwd) or die("连接数据库失败");
    mysql_select_db($dbname, $conn);
    mysql_query("set names 'UTF-8'");

    $result=mysql_query("SELECT * FROM $tbname");
    echo "<table>";
    echo "<tr>";
    echo "<th>A1单元格</th>";
    echo "<th>B1单元格</th>";
    echo "<th>C1单元格</th>";
    echo "<th>D1单元格</th>";
    echo "</tr>";
    echo "</table>";
    while($row=mysql_fetch_array($result)){
        //下面data改成你自己数据库对应的字段
        $data1 = $row["goods_name"];
        $data2 = $row["gongjinjia"];
        $data3 = $row["xinghao"];
        $data4 = $row["caizhi"];
        
        //输出字段的数据到单元格
        //下面data改成你自己数据库对应的字段
        echo "<table>";
        echo "<tr>";
            echo "<td>$goods_name</td>";
            echo "<td>$gongjinjia</td>";
            echo "<td>$xinghao</td>";
            echo "<td>$caizhi</td>";
        echo "</tr>";
        echo "</table>";
    }
?>
搜索更多相关主题的帖子: row php Undefined 单元格 echo 
2020-08-09 08:58
apull
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:三体星系
等 级:版主
威 望:185
帖 子:1404
专家分:8479
注 册:2010-3-16
得分:0 
//下面data改成你自己数据库对应的字段
        echo "<table>";
        echo "<tr>";
            echo "<td>$goods_name</td>";
            echo "<td>$gongjinjia</td>";
            echo "<td>$xinghao</td>";
            echo "<td>$caizhi</td>";

写得很明白,这4个变量没定义,你上面用的是$data1-$data4,这里又用这个变量名,这2处改成一样
2020-08-09 11:32
jingfang_434
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2009-10-24
得分:0 
回复 2楼 apull
谢谢您,确实是这里错了,改完后可以正常运行,非常感谢
2020-08-09 13:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-502781-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.318669 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved