Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 491
  • Last Modified:

Creating PDF files from a Mysql BD

Hello,
I have a simple script that lets users manage their address book online. I need to connect to that script an online PDF creation option, so that when they click on a button the Linux server generates a list of their addresses as PDF pages, ready to print on stickers for a postal mailing.
That server is recent and already support on-the-fly PDF creation on another site. (Add-on to OScommerce).
Is there a script I can use, what can I do to add that "export to PDF" feature ?

thank you.
0
Denisvt
Asked:
Denisvt
1 Solution
 
DenisvtAuthor Commented:
I have made progress, what I seem to need is actually a hint on how to write the proper PHP lines to fetch the data in my MySQL DB to feed it to the PDF generator.
Thanks for the links but like I said my server was PDF-ready.

Now I have a MySQL DB, structure / a dump can be provided if needed, and found that script :

<?php
define('FPDF_FONTPATH','font/');
require_once('PDF_Label.php');

$odbc = odbc_connect ('databasename', 'root', '') or die( "Could Not Connect to ODBC Database!" );

if(!($pdfinfo == "nada")){
$query = odbc_exec($odbc, stripslashes($pdfinfo)) or die (odbc_errormsg());

$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPage();
// Print labels
while($Row=odbc_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}

?>
However on my server it says
Fatal error: Call to undefined function: odbc_connect() in /home/httpd/vhosts/etc..

Can anybody advise ?

thanks !

0
 
hernst42Commented:
The odbc function are for connections with odbc-database-drivers. As you want to connect to a Mysql-Server youn need the mysql-functions:

mysql_connect
mysq_query
See http://de2.php.net/manual/en/function.mysql-db-name.php

e.g your code should look like (untested):

<?php
define('FPDF_FONTPATH','font/');
require_once('PDF_Label.php');

$odbc = mysql_connect ('root', '') or die( "Could Not Connect to ODBC Database!" );
mysql_select_db('databasename');

if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($pdfinfo), $odbc) or die (odbc_errormsg());

$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPage();
// Print labels
while($Row=mysql_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
gtkfreakCommented:
You need to install the php-odbc*.rpm file if you are using redhat or Fedora Core
0
 
DenisvtAuthor Commented:
Isn't there a way to simply use MySQL ?
I have almost found my answers based on the FPDF online PDF generator, it already generates the good PDF files with the layout of the labels I need, I seem to only need to find out how to read data from my MySQL data and send it to the pdf creation file.

I currently have, as a sample found in the pdf generator,

for($i=1;$i<=40;$i++)
      $pdf->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "Name $i", 'Test', 'av. sample', '90210', 'BH', 'USA'));

whereas I need the real data from my database to come there, by reading it in PHP from MySQL.
Any idea ?


0
 
ZenockCommented:
As noted your problem has absolutely nothing to do with generating pdf.   What you need to do is pay attention to hernst42.  He is absolutely correct.  

use the mysql_connect and mysql_query functions instead of using odbc.  It's faster too.
0
 
DenisvtAuthor Commented:
Do I understand incorrectly or do you mean the opposite ? You said
"use the mysql_connect and mysql_query functions instead of using odbc", which is precisely what I want as I do not have odbc not can install a new RPM on that Plesk server that hosts other important stuff.
0
 
hernst42Commented:
The code I posted is using mysql (the code you posted was using odbc) so i transformed the odbc_function the the mysql-pendant.
0
 
DenisvtAuthor Commented:
Ok I guess I must be close to what I need...
I had a problem with the "or die (odbc_errormsg());" part (gave me "Call to undefined function: odbc_errormsg()") so I replaced with a simple error display.

My code is now

<?php
define('FPDF_FONTPATH','font/');
require_once('PDF_Label.php');

$odbc = mysql_connect ("localhost", "dbname", "123") or die( "Could Not Connect to Database!" );
mysql_select_db('dbname');

if(!($pdfinfo == "nada")){
$query = mysql_query(stripslashes($pdfinfo), $odbc) or die("Something went wrong....");

$pdf2 = new PDF_Label('5160', 'mm', 1, 2);
$pdf2->Open();$pdf2->AddPage();
// Print labels
while($Row=mysql_fetch_array($query)){$pdf2->Add_PDF_Label(sprintf("%s\n%s\n%s\n%s, %s, %s", "", "$Row[Firstname] $Row[Lastname]", "$Row[Address1] $Row[Address2]", "$Row[City]","$Row[State]","$Row[Zip]"));}$pdf2->Output();
}
?>

but you get it I see "Something went wrong..."
What exactly ? ;-)
0
 
hernst42Commented:
The mysql_connect has other parameters than the odbc_connect so use:

$odbc = mysql_connect('host', 'dbuser', 'dbpass') or die( "Could Not Connect to Database!" .mysql_error());
mysql_select_db('dbname');
0
 
DenisvtAuthor Commented:
I have updated the script,
If I have

$query = mysql_query(stripslashes($pdfinfo), $odbc) or die("Something went wrong....");

I once again get the message "Something went wrong...",

if I use

$query = mysql_query(stripslashes($pdfinfo), $odbc) or die("Something went wrong...." .mysql_error());

then it displays a blank screen and does not create the PDF.
What did I miss ?...
Thanks,
0
 
hernst42Commented:
$pdfinfo must be a vaild sql-statement
0
 
DenisvtAuthor Commented:
Sorry that was not perfectly clear to me;
I have now tried adding the search statement there and have managed to have my script working perfectly, thanks a lot for that precious help !
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now