Php – Crystal Reports 11.5 with PHP and MySQL

crystal-reportsMySQLPHPreporting-services

I am new to Crystal Reports, and I am using version Crystal Reports 11.5.

My requirement is as below:

  1. Programming Environment is PHP .
  2. Database is MySQL.
  3. I want to generate PDF reports using Crystal Report + PHP + MySQL.

Currently, I am using COM object to connect to Crystal Report using PHP, and I am able to generate a sample static PDF report.

My main task is to do all processing in PHP by fetching values from MySQL and pass the values to Crystal Reports and generate a PDF. I need assistance to achieve this task. If anybody can provide sample code then it will be much better.

This is what I have so far:

$my_report = "E:\\xampp\\htdocs\\crystal\\Test1.rpt"; 

$my_pdf = "E:\\xampp\\htdocs\\crystal\\test.pdf"; 

$o_CrObjectFactory = new COM('CrystalReports11.ObjectFactory.1');

// Create the Crystal Reports Runtime Application.


$o_CrApplication =$o_CrObjectFactory->CreateObject("CrystalDesignRunTime.Application"); 

//------ Open your rpt file ------ 

$creport = $o_CrApplication->OpenReport($my_report, 1); 

//------ Connect to DB2 DataBase ------ 

**this is the hard part where I am not able to complete connection to mysql**
$o_CrApplication->LogOnServer('which library','mlims','root',''); 

//------ Put the values that you want -------- 

$creport->RecordSelectionFormula="{parameter.id}='1'"; 

//------ This is very important. DiscardSavedData make a 

// Refresh in your data -------

$creport->DiscardSavedData; 

//------ Read the records :-P ------- 

$creport->ReadRecords(); 

//------ Export to PDF ------- 

$creport->ExportOptions->DiskFileName=$my_pdf; 
$creport->ExportOptions->FormatType=31; 
$creport->ExportOptions->DestinationType=1; 
$creport->Export(false); 

//------ Release the variables 
$creport = null; 
$crapp = null; 
$ObjectFactory = null; 

As you see In the above code, I need to connect the Mysql server which I have been trying to do for the last few days. I have tried many examples on the net, but most of them are for SQL Server, not MySQL.

Best Answer

Please follow the steps:

  1. Download the MySQL Connector J jar file. That download should contain a jar file that looks something like: mysql-connector-java-3.1.14-bin.jar

  2. Add the location of your newly downloaded jar file to the Classpath, as defined in CrystalReports CRConfig.xml file. On a Windows machine, the config file will be located somewhere like: C:\Program Files\Business Objects\Common\3.5\java\CRConfig.xml

  3. Once you have altered your CRConfig.xml, close and reopen Crystal Reports.

  4. From the menu: File -> New -> Standard Report
  5. In the “Available Data Sources” list, double-click to expand “Create New Connection”
  6. Double-click to expand “JDBC (JNDI)”
  7. Double-click “Make New Connection”
  8. Connection URL: “jdbc:mysql://db.example.com/dbname” (Use your own database host name and db name). Database Classname: “com.mysql.jdbc.Driver”
  9. Click “Next”
  10. Enter a database user/password combination when prompted.

You should now be able to inspect the tables/columns in the database to begin reporting.

See reference

Related Topic