繁体   English   中英

PHP-MYSQL:无法从数据库中将多个记录提取到Word文件中

[英]PHP-MYSQL: Not able to fetch multiple record from database into word file

我必须将记录从数据库下载到Word文件中。 但是我不能这样做,因为每当我在表周围添加while循环时,都会给我错误。

当我获取单个记录时,我的代码工作正常。 有人可以为我提供获取多个记录的方法吗?

这是我的代码:

  <?php


   include('dbcon.php');

   if($_GET['id']!='')

$id=$_GET['id'];

    $party_name=mysql_query("select * FROM tb_party Where party_id='".$id."' ");
    $party_name_row=mysql_fetch_array($party_name);


    $sample_text1 = $party_name_row['party_name'];




        $party_details="select * from tb_party_rate_entry
                    where party_id='$id'";
                    $query_details=mysql_query($party_details);
                    $row_details=mysql_fetch_array($query_details);



                    $word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
                    xmlns:w='urn:schemas-microsoft-com:office:word'
                    xmlns='http://www.w3.org/TR/REC-html40′";

                   $word_xml_settings = "<xml><w:WordDocument>
                   <w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";

                   $word_landscape_style = 
                   "@page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
                    }  div.Section1{page:Section1;}";

                   $word_landscape_headinh="{font-size:15px; }";


                  $word_landscape_div_imf='
                 <img  src="http://silverevents.net/user_image/' .$sample_text1 . '"  
                       height="150" width="150"/>';



                     $word_landscape_div_start = "<div class='Section1′>";

                  $word_landscape_div_end = "</div>";

                  $content = '<html '.$word_xmlns.'>
        <head>
                     <title>Party Rate Report</title>'
                     .$word_xml_settings.'<style type="text/css">
        '.$word_landscape_style.' table,td 
                     {border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
        </head>
        <body>'.$word_landscape_div_start  . 
                    '<table style="width:500px; color:#000000; 
                    margin:0 0 50px 100px; font-family: Arial Black, Gadget, 
                    sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
                    </tr></table></br>

        <table style="width:700px;font-size:15px;font-family: 
                     Arial Black, Gadget, sans-serif;">

        <tr>

        <td width="180">
            Party Name : '.  $party_name_row['party_name'] . '
        </td>

        <td width="250">
        Address : '.  $party_name_row['party_address'] . '
        </td>

        <td width="150">
        Phone : '.   $party_name_row['phone_no']. '
        </td>
        </tr>


        </table><br/>   
        <table style="width:700px; color:#00000; margin:22px 0 0 0px; 
                     font- family:Helvetica, sans-serif, Verdana; 
                    font-size:15px;  border1px solid #666;">
        <tr>
        <td style=" border:1px solid #666; width:300px;"> Zone</td>
        <td style=" border:1px solid #666; width:300px;"> 
                     Delivery Mode          
                     </td>
        <td style=" border:1px solid #666; width:300px;"> Doc Type </td>
        <td style=" border:1px solid #666; width:300px;"> Weight slot </td>
        <td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
        <td style=" border:1px solid #666; width:300px;"> Flat </td>
        <td style=" border:1px solid #666; width:300px;"> Price </td>
        </tr>

        <tr>
         <td style=" border:1px solid #666; width:300px;">' .
                     $row_details['zone_id']. '</td>
                   <td style=" border:1px solid #666; width:300px;">'.
                    $row_details['delivery_mode_id'].'
                   </td>
                 <td style=" border:1px solid #666; width:300px;">'. 
                  $row_details['doc_type_id'].'</td>    
                 <td style=" border:1px solid #666; width:300px;">'. 
                   $row_details['weight_slot_id'].'</td>    
                 <td style=" border:1px solid #666; width:300px;">'.
                 $row_details['weight_measure_id'].'</td>   
           <td style=" border:1px solid #666; width:300px;">'.
              $row_details['flat'].'</td>   
           <td style=" border:1px solid #666; width:300px;">'.   
            $row_details['price' ] . '</td>         
        </tr>


        </table></br>' . $word_xml_settings. $word_landscape_div_end.'
        </body>
        </html>';

 @header('Content-Type: application/msword');
 @header('Content-Length: '.strlen($content));
 @header('Content-disposition: inline; filename="Party Rate Report.doc"');
 echo $content;

  ?>

这是我的数据库:

在此处输入图片说明

做这样的事情:

while ($row_details=mysql_fetch_array($query_details)) {
    //do row specific code
}

快速重新格式化,添加一个循环(并假设id为整数):-

<?php


    include('dbcon.php');

    if($_GET['id']!='')
    {
        $id=intval($_GET['id']);

        $party_name=mysql_query("select * FROM tb_party Where party_id=$id ");
        if ($party_name_row=mysql_fetch_array($party_name))
        {
            $sample_text1 = $party_name_row['party_name'];
        }

        $word_xmlns = "xmlns:o='urn:schemas-microsoft-com:office:office'
        xmlns:w='urn:schemas-microsoft-com:office:word'
        xmlns='http://www.w3.org/TR/REC-html40'";

        $word_xml_settings = "<xml><w:WordDocument>
        <w:View>Print</w:View><w:Zoom>100</w:Zoom></w:WordDocument></xml>";

        $word_landscape_style = 
        "@page {size:8.5in 11.0in; margin:0.5in 0.31in 0.42in 0.25in;
        }  div.Section1{page:Section1;}";

        $word_landscape_headinh="{font-size:15px; }";

        $word_landscape_div_imf='
        <img  src="http://silverevents.net/user_image/' .$sample_text1 . '"  
        height="150" width="150"/>';

        $word_landscape_div_start = "<div class='Section1'> ";

        $word_landscape_div_end = "</div>";

        $content = '<html '.$word_xmlns.'>
            <head>
                         <title>Party Rate Report</title>'
                         .$word_xml_settings.'<style type="text/css">
            '.$word_landscape_style.' table,td 
                         {border:1px solid #FFFFFF; width:200px; color:#CCC;} </style>
            </head>
            <body>'.$word_landscape_div_start  . 
                        '<table style="width:500px; color:#000000; 
                        margin:0 0 50px 100px; font-family: Arial Black, Gadget, 
                        sans-serif; font-size:24px;"><tr><td> Party Rate Report </td>
                        </tr></table></br>

            <table style="width:700px;font-size:15px;font-family: 
                         Arial Black, Gadget, sans-serif;">

            <tr>

            <td width="180">
                Party Name : '.  $party_name_row['party_name'] . '
            </td>

            <td width="250">
            Address : '.  $party_name_row['party_address'] . '
            </td>

            <td width="150">
            Phone : '.   $party_name_row['phone_no']. '
            </td>
            </tr>


            </table><br/>   
            <table style="width:700px; color:#00000; margin:22px 0 0 0px; 
                         font- family:Helvetica, sans-serif, Verdana; 
                        font-size:15px;  border1px solid #666;">
            <tr>
            <td style=" border:1px solid #666; width:300px;"> Zone</td>
            <td style=" border:1px solid #666; width:300px;"> 
                         Delivery Mode          
                         </td>
            <td style=" border:1px solid #666; width:300px;"> Doc Type </td>
            <td style=" border:1px solid #666; width:300px;"> Weight slot </td>
            <td style=" border:1px solid #666; width:300px;"> Weight Unit </td>
            <td style=" border:1px solid #666; width:300px;"> Flat </td>
            <td style=" border:1px solid #666; width:300px;"> Price </td>
            </tr>';


        $party_details="select * 
                        from tb_party_rate_entry
                        where party_id=$id";
        $query_details=mysql_query($party_details);
        while($row_details=mysql_fetch_array($query_details))
        {
            $content .= '<tr>
             <td style=" border:1px solid #666; width:300px;">' .
                         $row_details['zone_id']. '</td>
                       <td style=" border:1px solid #666; width:300px;">'.
                        $row_details['delivery_mode_id'].'
                       </td>
                     <td style=" border:1px solid #666; width:300px;">'. 
                      $row_details['doc_type_id'].'</td>    
                     <td style=" border:1px solid #666; width:300px;">'. 
                       $row_details['weight_slot_id'].'</td>    
                     <td style=" border:1px solid #666; width:300px;">'.
                     $row_details['weight_measure_id'].'</td>   
               <td style=" border:1px solid #666; width:300px;">'.
                  (($row_details['flat'] != '') ? $row_details['flat'] : ' ').'</td>   
               <td style=" border:1px solid #666; width:300px;">'.   
                $row_details['price' ] . '</td>         
            </tr>';


        }
        $content .=     '</table></br>' . $word_xml_settings. $word_landscape_div_end.'
            </body>
            </html>';

        @header('Content-Type: application/msword');
        @header('Content-Length: '.strlen($content));
        @header('Content-disposition: inline; filename="Party Rate Report.doc"');
        echo $content;
    }

  ?>

请注意,对于新代码,您可能应该切换为使用mysqli而不是php mysql驱动程序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM