<?php
mysql_connect('localhost','root','root');
mysql_select_db('project');
$dbname="project"
require('fpdf.php');

//Connect to your database
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");


//Create new pdf file
$pdf=new FPDF();

//Open file
$pdf->Open();

//Disable automatic page break

//Add first page
$pdf->AddPage();

//set initial y axis position per page
$y_axis_initial = 25;


$pdf->SetX(25);
$pdf->Cell(30, 6, 'id', 1, 0, 'L', 1);
$pdf->Cell(30, 6, 'Studentid', 1, 0, 'R', 1);

//Select the Products you want to show in your PDF file
$result=mysql_query('select * from add_marks');

//initialize counter
//Set maximum rows per page
//Set Row Height

while($row = mysql_fetch_array($result))
{
        //print column titles for the current page
        $pdf->SetY($y_axis_initial);
        $pdf->SetX(25);
        $pdf->Cell(30, 6, 'id', 1, 0, 'L', 1);

        $pdf->Cell(30, 6, 'Studentid', 1, 0, 'R', 1);

        //Go to next row
    $id = $row['id'];
    $Studentid = $row['Studentid'];
    //Go to next row
}

//Create file
$pdf->Output();
?>

<?php
$result = mysql_query("SHOW COLUMNS FROM add_marks");

 $i = 0;

if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result))
{

$csv_output .='"'.$row['id'].'","';

$i++;}
}
$csv_output .= "n";
$values = mysql_query("SELECT * FROM add_marks");

while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";

}
$csv_output .= "n";
}
$filename = $file."_".date("d-m-Y_H-i",time());


header("Content-type: application/pdf");
header("Content-disposition: pdf" . date("Y-m-d") . ".pdf");
header( "Content-disposition: filename=".$viewresults1.php".pdf");

print $csv_output;

exit;
?>

**jst help me to convert database record to pdf file format and then display the pdf file on the form i hv tried the abv cord bt it does nt work db table is the same as i mentioned earlier pls give me the result as fst as possible *

Dani AI

Generated

Brief, practical summary and working approach for converting a database table to a PDF and showing it in the browser. The original post by mixes multiple connections, CSV output and PDF output, which leads to headers and output being sent in the wrong order; was right to point out the connection duplication. The safe sequence is: open a single DB connection (use PDO or mysqli), fetch rows, render those rows into the PDF library (FPDF/TCPDF/mPDF), ensure no other output is sent, then stream the PDF to the browser.

Minimal, reliable pattern (uses PDO + FPDF). This example is intentionally different from the code already posted in the thread:

<?php
require 'fpdf.php';
$pdo = new PDO('mysql:host=localhost;dbname=project;charset=utf8','user','pass',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->query('SELECT id, Studentid, name FROM add_marks');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','',10);
$pdf->Cell(30,8,'ID',1);
$pdf->Cell(50,8,'StudentID',1);
$pdf->Cell(70,8,'Name',1);
$pdf->Ln();

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  $pdf->Cell(30,6,$row['id'],1);
  $pdf->Cell(50,6,$row['Studentid'],1);
  $pdf->Cell(70,6,$row['name'],1);
  $pdf->Ln();
}

ob_end_clean(); // clear any buffered output
$pdf->Output('I','results.pdf'); // 'I' = inline, 'D' = download, 'F' = save to file
exit;
?>

Troubleshooting tips: remove any echo/print (the CSV code in the OP), test by saving the PDF to disk first with Output('F', '/tmp/test.pdf') to verify generation, ensure no UTF-8 BOM or stray whitespace before <?php, and enable full error reporting while debugging. For production, store DB credentials outside the web root, use prepared statements for safety, and use TCPDF/mPDF if UTF-8 or complex layouts are needed.

k.. so first off, you are connecting to your database twice, for no reason

mysql_connect('localhost','root','root');mysql_select_db('project');$dbname="project"require('fpdf.php');//Connect to your databasemysql_connect("$host", "$username", "$password")or die("cannot connect");mysql_select_db("$db_name")or die("cannot select DB");

all that could be simplified...

$usr = 'usrname';
$pw = 'pw';
$host = 'localhost';
$dbname = "project";

$db = mysql_connect($host, $usr, $pw);
mysql_select_db($dbname, $db) or die ('Could not connect to MySQL: '.mysql_error());

Most of that should be in an include file, that is above your root.

Then, include_once('fpdf.php'); is fine...

You create your pdf file as per the fpdf object...

After that... Im having a little trouble following what your code is trying to accomplish...

you have

$filename = a non existent $file variable
and

you have a header that refers to a non existent variable $viewresults1.php... which... Im not even quite sure what to make of....

A little more info would be helpful in order to help you...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.