我正在使用php& MySQL的。
我想从数据库中的表中检索所有记录并将它们打印到pdf文件中。
我编写了一些代码,但它只检索表中的第一条记录而没有检索到所有其他记录。
这是我的代码:
<?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>";
?>
答案 0 :(得分:1)
<?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>";
?>
答案 1 :(得分:0)
首先,您需要创建一个数组,然后将所有数据设置到数组中
让您的$pdf->Write();
评论有效。
你不能这样写,因为所有数据都在。
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.
它只检索第一条记录,因为您需要获取所有数据集一次! 但是你在while循环中写了pdf,因此它只检索第一个数据而不能传递给另一个数据。