NOTE: Flexmonster Data Compressor is considered deprecated, so the approach described below is not recommended for connecting to SQL databases.
To see the relevant way of connecting to relational databases, refer to this article.
If Flexmonster is not yet embedded, set up an empty component in your webpage:
Complete the Integrating Flexmonster guide. Your code should look similar to the following example:
let pivot = new Flexmonster({ container: "pivotContainer", componentFolder: "node_modules/flexmonster/", toolbar: true });
Complete the Integration with React guide. Your code should look similar to the following example:
<FlexmonsterReact.Pivot toolbar={true} />
Complete the Integration with Angular guide. Your code should look similar to the following example:
<fm-pivot [toolbar]="true"> </fm-pivot>
Complete the Integration with Vue guide. Your code should look similar to the following example:
<Pivot toolbar />
Add the following dependency to your project: flexmonster-compressor.php
. It is located in the Pivot Table for Databases/server/php/
folder of the download package:
require_once("flexmonster-compressor.php");
Below is a connection and query sample for a MySQL database:
mysql_connect($server, $username, $password); mysql_select_db($dbname); $result = mysql_query("SELECT * FROM customer");
Then the following line of code will start streaming a $result
:
header('Content-Type: text/plain'); Compressor::compressMySql($result);
The full project is available at Pivot Table for Databases/server/php/
inside the download package.
By default, the browser prevents JavaScript from making requests across domain boundaries. CORS allows web applications to make cross-domain requests. Here are some useful links explaining how to setup CORS on your server:
Alternatively, you can add the following headers at the beginning of PHP file:
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods', 'OPTIONS,GET,PUT,POST,DELETE'); header('Access-Control-Allow-Headers', 'Content-Type');
Now it’s time to configure the pivot table on the webpage. Let’s create a minimal report for this (replace filename
and other parameters with your specific values):
var pivot = new Flexmonster({ container: "pivotContainer", toolbar: true, report: { dataSource: { type: "csv", /* URL to the Data Compressor PHP */ filename: "http://localhost/demo-compress-mysql.php" } }, licenseKey: "XXXX-XXXX-XXXX-XXXX-XXXX" });
Launch the webpage from a browser — there you go! A pivot table is embedded into your project. Check out the example on JSFiddle.
Here you can find a few PHP examples of compressing different databases.
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); mysql_connect("localhost", "username", "password"); mysql_select_db("foodmart"); $result = mysql_query("SELECT * FROM customer"); Compressor::compressMySql($result);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); $mysqli = new mysqli("localhost", "username", "password", "foodmart"); $result = $mysqli->query("SELECT * FROM customer"); Compressor::compressMySqli($result);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); $conn = sqlsrv_connect("localhost", array( "Database"=>"foodmart", "UID"=>"username", "PWD"=>"password" ) ); $result = sqlsrv_query($conn, "SELECT * FROM customer"); Compressor::compressSQLSRV($result);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); $conn = pg_connect("host=localhost dbname=foodmart user=username password=password"); $result = pg_query($conn, "SELECT * FROM customer"); Compressor::compressPostgreSQL($result);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); $conn = oci_connect("username", "password", "localhost/XE"); $stid = oci_parse($conn, 'SELECT * FROM customer'); oci_execute($stid); Compressor::compressOCI($stid);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); $conn = new PDO("mysql:host=localhost;dbname=foodmart", "username", "password"); $result = $conn->query("SELECT * from customer"); Compressor::compressPDO($result);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); mysql_connect("localhost", "username", "password"); mysql_select_db("foodmart"); $result = mysql_query("SELECT * FROM customer"); $header = array(); for ($i=0; $i < mysql_num_fields($result); $i++) { $header[$i] = array( 'name' => mysql_field_name($result, $i), 'type' => mysql_field_type($result, $i) ); } $array = array(); $array[] = $header; while ($row = mysql_fetch_row($result)) { $array[] = $row; } Compressor::compressArray($array);
header("Content-Type: text/plain"); require_once("flexmonster-compressor.php"); mysql_connect("localhost", "username", "password"); mysql_select_db("foodmart");
mysql_set_charset("utf8"); $result = mysql_query("SELECT * FROM customer"); $outFile = "data.csv"; Compressor::compressMySql($result, $outFile);
It is likely that output_buffering
is enabled in the PHP configuration on your server and therefore data is not streaming. Try disabling it in php.ini
for better performance:output_buffering = Off
(see https://php.net/manual/en/outcontrol.configuration.php).
You may be interested in the following articles: