1

I have the task of generating the excel sheet of each and every student separately so I used PHPExcel lib to perform the task

<?php
    $host='localhost'; $user='root'; $pass=''; $DataBase='college';//define the correct values
    // open the connexion to the databases server
    $Link=@mysqli_connect($host,$user,$pass,$DataBase) or die('Can\'t connect !');
    mysqli_set_charset($Link, 'utf8');//if not by default
    //your request
    if(isset($_GET['stud_id'])){
        $id=$_GET['stud_id'];

        $SQL='SELECT * from stud_master where stud_id=$id';
        $rs=mysqli_query($Link, $SQL);//get the result (ressource)
        /** Include PHPExcel */
        require_once 'ec/Classes/PHPExcel.php';//change if necessary

        // Create new PHPExcel object
        $objPHPExcel = new PHPExcel();
        $F=$objPHPExcel->getActiveSheet();
        $Line=1;
        while($Trs=mysqli_fetch_assoc($rs)){//extract each record
            $F->
                setCellValue('A'.$Line, $Trs['stud_id'])->
                setCellValue('B'.$Line, $Trs['course_id'])->
                setCellValue('C'.$Line, $Trs['fname'])->
                setCellValue('D'.$Line, $Trs['mname'])->
                setCellValue('E'.$Line, $Trs['lname']);//write in the sheet
            ++$Line;
        }
    }
    // Redirect output to a client’s web browser (Excel5)
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="report.xls"');
    header('Cache-Control: max-age=0');

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;

enter image description here

dethtron5000
  • 10,363
  • 1
  • 31
  • 32
  • It looks like your script is generating a warnings for your MySQL access, so fix that – Mark Baker Apr 17 '15 at 10:46
  • why these random characters are generated when i try to generate excel sheet of whole table it works fine but when i use it for individual id it fails... –  Apr 17 '15 at 10:49
  • 2
    Those "random" characters are the actual Excel file bytestream.... but because you have those plaintext warnings in the output as well, they're corrupting the Excel file..... fix those warnings.... start with fixing the `mysql_fetch_assoc()` problem, and the rest should fix itself – Mark Baker Apr 17 '15 at 10:51
  • If `$id` isn't set because `$_GET['stud_id']` wasn't set, you're trying to inject a non-existent variable into your SQL, this will generate warnings.... there's one problem you need to fix – Mark Baker Apr 17 '15 at 10:54
  • Thnks Mark for your valuable suggestion that one warning is corrupting the excel file –  Apr 17 '15 at 10:59
  • Updated formatting and indentation for readibility. – dethtron5000 Apr 20 '15 at 13:40

1 Answers1

0

Seems, the you have an error in your SQL syntax:

//use double quotes here, not single - otherwise $id won't be substituted
$SQL = "SELECT * from stud_master where stud_id=$id";
$rs=mysqli_query($Link, $SQL);//get the result (ressource)

But better use prepared statements to protect from SQL injection.

user4035
  • 22,508
  • 11
  • 59
  • 94
  • thnk 4035 i pointed out the mistake can u tell me more when to use single quote and when double i generally see many people using single quote –  Apr 17 '15 at 11:01
  • 1
    @Optimmus Here is the most basic difference: http://stackoverflow.com/a/3446245/789186 – user4035 Apr 17 '15 at 11:20