0

I'm using php & mysql.

I want to retrieve all the records from a table in the database and print them into pdf file.

I write some code but it retrieves only the first record in the table and not retrieved all the other records.

Here is my code:

<?php
$con = mysql_connect("localhost","root","")
    or die("Failed to connect to the server: " . mysql_error());
mysql_select_db("Users") 
    or die("Failed to connect to the database: " . mysql_error());

$query = mysql_query("SELECT * FROM reservations");
$numrows = mysql_num_rows($query);
if($numrows > 0)
{
    while($result = mysql_fetch_assoc($query))
    {
        $db_id       = $result['id'];
        $db_fname    = $result['fname'];
        $db_lname    = $result['lname'];
        $db_email    = $result['email'];
        $db_phone    = $result['phone'];
        $db_day      = $result['day'];
        $db_time     = $result['time'];
        $db_cartype  = $result['car_type'];
        $db_carcolor = $result['car_color'];
        $db_carmodel = $result['car_year'];
        $db_carno    = $result['car_no'];
        $db_problem  = $result['problem'];
        $db_status   = $result['status'];
        $db_remtime  = $result['remaining_time'];
        $db_cost     = $result['cost'];
        $db_payment  = $result['payment'];
        $db_date     = $result['date'];

        require("FPDF/fpdf.php");
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont("Arial","B","15");
        $pdf->Cell(0,10,"Freedom Car: Total Reservations",1,1,"C");
        $pdf->Image("porchue.jpg");
        $pdf->SetFont("Arial","","10");
        $pdf->Write(5,"ID: $db_id\n");
        $pdf->Write(5,"First Name: $db_fname\n");
        $pdf->Write(5,"Last Name: $db_lname\n");
        $pdf->Write(5,"Email Address: $db_email\n");
        $pdf->Write(5,"Phone Number: $db_phone\n");
        $pdf->Write(5,"Reserved Day: $db_day\n");
        $pdf->Write(5,"Reserved Time: $db_time\n");
        $pdf->Write(5,"Car Type: $db_cartype\n");
        $pdf->Write(5,"Car Color: $db_carcolor\n");
        $pdf->Write(5,"Car Model: $db_carmodel\n");
        $pdf->Write(5,"Car Number: $db_carno\n");
        $pdf->Write(5,"Car Problem: $db_problem\n");
        $pdf->Write(5,"Car Status: $db_status\n");
        $pdf->Write(5,"Remaining Time: $db_remtime\n");
        $pdf->Write(5,"Total Cost: $db_cost\n");
        $pdf->Write(5,"Payment Method: $db_payment\n");
        $pdf->Write(5,"Reservation Date: $db_date\n\n");
        $pdf->Output();
    }
}
else
    echo "<h4>Sorry, No reservations at this time.</h4>";
?>
2
  • 2
    look at what should and shouldn't be inside that while loop Commented Apr 27, 2015 at 21:55
  • I don't understand you, please explain your idea in code not in just some text. @Dagon Commented Apr 27, 2015 at 22:08

2 Answers 2

2
<?php
$con = mysql_connect("localhost","root","")
    or die("Failed to connect to the server: " . mysql_error());
mysql_select_db("Users") 
    or die("Failed to connect to the database: " . mysql_error());

$query = mysql_query("SELECT * FROM reservations");
$numrows = mysql_num_rows($query);
if($numrows > 0)
{
    require("FPDF/fpdf.php");
        $pdf = new FPDF();
        $pdf->AddPage();
        $pdf->SetFont("Arial","B","15");
        $pdf->Cell(0,10,"Freedom Car: Total Reservations",1,1,"C");
        $pdf->Image("porchue.jpg");
        $pdf->SetFont("Arial","","10");
    while($result = mysql_fetch_assoc($query))
    {
        $db_id       = $result['id'];
        $db_fname    = $result['fname'];
        $db_lname    = $result['lname'];
        $db_email    = $result['email'];
        $db_phone    = $result['phone'];
        $db_day      = $result['day'];
        $db_time     = $result['time'];
        $db_cartype  = $result['car_type'];
        $db_carcolor = $result['car_color'];
        $db_carmodel = $result['car_year'];
        $db_carno    = $result['car_no'];
        $db_problem  = $result['problem'];
        $db_status   = $result['status'];
        $db_remtime  = $result['remaining_time'];
        $db_cost     = $result['cost'];
        $db_payment  = $result['payment'];
        $db_date     = $result['date'];


        $pdf->Write(5,"ID: $db_id\n");
        $pdf->Write(5,"First Name: $db_fname\n");
        $pdf->Write(5,"Last Name: $db_lname\n");
        $pdf->Write(5,"Email Address: $db_email\n");
        $pdf->Write(5,"Phone Number: $db_phone\n");
        $pdf->Write(5,"Reserved Day: $db_day\n");
        $pdf->Write(5,"Reserved Time: $db_time\n");
        $pdf->Write(5,"Car Type: $db_cartype\n");
        $pdf->Write(5,"Car Color: $db_carcolor\n");
        $pdf->Write(5,"Car Model: $db_carmodel\n");
        $pdf->Write(5,"Car Number: $db_carno\n");
        $pdf->Write(5,"Car Problem: $db_problem\n");
        $pdf->Write(5,"Car Status: $db_status\n");
        $pdf->Write(5,"Remaining Time: $db_remtime\n");
        $pdf->Write(5,"Total Cost: $db_cost\n");
        $pdf->Write(5,"Payment Method: $db_payment\n");
        $pdf->Write(5,"Reservation Date: $db_date\n\n");
    }
        $pdf->Output();
}
else
    echo "<h4>Sorry, No reservations at this time.</h4>";
?>
Sign up to request clarification or add additional context in comments.

1 Comment

I review my code and discovered the problem. thank you sir for your help. checking while loop was the problem. and this is the answer. @Dagon my welcomes to you from egypt.
0

firstly you need to create an array and set all data to the array after that make your $pdf->Write(); comment work. you cannot write like this because all data in while.

 while($result = mysql_fetch_assoc($query))
    {
// create an array or call all data here !
}
// use forachone array i main kame loop and start your code.

it retrieves only the first record because you need to get all data set once ! but you writepdf commant in the while loop so it retrieves only the first data and cannot pass to another data.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.