Pricing changes are coming in January. Contact our Sales team to secure the current price for your desired license.

Connecting to a relational database with PHP

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.

Requirements

  • Flexmonster Pivot version 2.213 or higher
  • PHP 5.3+ (earlier versions may also work, but have not been tested)

Supported databases

Step 1: Embed the component into your webpage

If Flexmonster is not yet embedded, set up an empty component in your webpage:

In pure JavaScript

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
});

In React

Complete the Integration with React guide. Your code should look similar to the following example:

<FlexmonsterReact.Pivot
 toolbar={true}
/>

In Angular

Complete the Integration with Angular guide. Your code should look similar to the following example:

<fm-pivot
 [toolbar]="true">
</fm-pivot>

In Vue

Complete the Integration with Vue guide. Your code should look similar to the following example:

<Pivot
 toolbar
/>

Step 2: Setup the Data Compressor on the server

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.

Step 3: Enable cross-origin resource sharing (CORS)

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');

Step 4: Configure the report with your own data

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.

Examples

Here you can find a few PHP examples of compressing different databases.

MySQL

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);

MySQLi

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);

Microsoft SQL Server

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);

PostgreSQL

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);

Oracle Database

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);

PDO - a MySQL example

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);

Other Databases - a MySQL example

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);

Saving to a file - a MySQL example

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);

Troubleshooting

Data is not streaming

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).

What's next?

You may be interested in the following articles: