-1

i am coming across an error while trying to export data from MYSQL to PDF using PHP

Here's my php code:

<?php

require('fpdf.php');
include ('connection.php');

$data = mysql_query("SELECT * FROM sold WHERE imei = '87712839712893'");
$result = mysql_fetch_array($data);

$saledate = $result['saledate'];
$price = $result['sellingprice'];

$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('arial','B',10);
$pdf->Cell(40,10,'$saledate');
$pdf->SetFont('arial','B',30);
$pdf->Cell(40,10,'$price');
$pdf->Output();

?>

Here's the ERROR

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\POS\v2\tuto1.php on line 7
FPDF error: Some data has already been output, can't send PDF file
Tomasz Kowalczyk
  • 10,385
  • 6
  • 53
  • 66
darsh
  • 13
  • 2
  • 10

3 Answers3

2

I believe you'll need to use mysql_fetch_object, not mysql_fetch_array. The latter will only do numeric indices. (I think that mysql_fetch_assoc will work as well.)

I don't know if you need to do this if you are retrieving only one row, but I usually wrap the mysql fetch results in a while loop. I would rewrite your code like so:

require('fpdf.php');
include ('connection.php');

$pdf=new FPDF();
$pdf->AddPage();

$data = mysql_query("SELECT * FROM sold WHERE imei = '87712839712893'");

while ($result = mysql_fetch_assoc($data))
(
    $pdf->SetFont('arial','B',10);
    $pdf->Cell(40,10,'$result["saledate"]'); // edit: Note the double quotes. The error was probably caused by terminating the string early.
    $pdf->SetFont('arial','B',30);
    $pdf->Cell(40,10,'$result["sellingprice"]');
}

// I don't know if the pdf Cell() command numeric arguments need to change or not. If they do, change them.

$pdf->Output();

?>

This has the added value of letting you query more than one row in your table to output to PDF.

Other than that, check your SQL statement and make sure that it's correct.

jedd.ahyoung
  • 7,892
  • 7
  • 51
  • 93
  • thanks i tried exactly your way but its giving a parse error: Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\POS\v2\tuto1.php on line 37 – darsh Jun 08 '11 at 17:56
  • @Darsh That's a syntax error - I think I know what the issue is. Edited my answer. However, an error like that should be fairly easy to spot - did you try editing the code at all? – jedd.ahyoung Jun 08 '11 at 18:05
  • thanks for the suggestion. I did not do any change other than replacing the curved bracked with open curly bracket. Also, I did edited the code with double quotes its generating a PDF but not pulling out the values from the MYSQL database its literally printing on pdf page no values: $result["saledate"] $result["sellingprice"] – darsh Jun 08 '11 at 18:16
0

mysql_query() returns resource on success and status of query on failure. It returned false because query failed for some reason. Test the query result before passing it to mysql_fetch_array() and display problem error message.

Tomasz Kowalczyk
  • 10,385
  • 6
  • 53
  • 66
  • i got it working and figured out the problem its pulling a PDF file now but the value of $saledate & $price is not coming rather its only printing $saledate & $price. can you suggest?? – darsh Jun 08 '11 at 17:35
  • Do `var_dump($result);` and see what it returns. Assing variables accordingly. – Tomasz Kowalczyk Jun 08 '11 at 17:37
0

SELECT * FROM sold WHERE imei = '87712839712893' is returning an error. E.g., query syntax is wrong or ambiguous, and is returning a bool. Before your foreach, do this:

if(!$result){
    throw new Exception('Error: ' . mysql_error());
}

Be sure to catch the exception before executing any code that is dependent on the results of your query.

wanovak
  • 6,079
  • 23
  • 32
  • Downvoted because `die()` isn't really a good error-handling method. I would suggest something more robust - or at least something that doesn't halt PHP and break the page for any users. There's an excellent article about error handling [here](http://www.phpfreaks.com/blog/or-die-must-die). – jedd.ahyoung Jun 08 '11 at 17:51
  • thanks its solved as the include connection file was not connecting so I typed the mysql_connect and database select right after require field. – darsh Jun 08 '11 at 17:53
  • @lunchmeat317 Good read, thanks. I'll be keeping my post the same so your comment remains relevant. – wanovak Jun 09 '11 at 14:10
  • Nah - if you can make it better, go ahead and edit it and I'll remove the downvote. Users can view the edit history if they want to see the original context. – jedd.ahyoung Jun 09 '11 at 16:15