How to Generate Interactive Map Settings from an SQL Database using PHP

This tutorial will guide you through the process of generating settings for an interactive map using data from an SQL database with PHP. We will use the HTML5 USA Map as an example. By the end of this tutorial, you will be able to dynamically generate map settings using information stored in a database.

US Map for websites


  • Basic understanding of PHP and SQL
  • Familiarity with HTML and JavaScript
  • Access to an SQL database containing map data (state settings)

Step 1: Set up the database connection

Create a new PHP file (e.g., "map_data.php") and include your database connection settings. Replace the placeholders with your database credentials.

$servername = "your_server_name";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);

Step 2: Fetch map data from the database

Next, write a query to fetch the map data (state settings) from the database.

Screenshot of a table from the database
Screenshot of a database table

Assuming your table is named "states", you can use the following query:

$sql = "SELECT id, name, shortname, link, comment, color, color_over FROM states";
$result = $conn->query($sql);

Step 3: Generate JavaScript map settings

Iterate through the fetched data and generate the map settings using the database column names.

if ($result->num_rows > 0) {
  echo "var map_data = {";
  while($row = $result->fetch_assoc()) {
    echo "st" . $row["id"] . ": {";
    echo "id: " . $row["id"] . ",";
    echo "name: '" . addslashes($row["name"]) . "',";
    echo "shortname: '" . addslashes($row["shortname"]) . "',";
    echo "link: '" . addslashes($row["link"]) . "',";
    echo "comment: '" . addslashes($row["comment"]) . "',";
    echo "color: '" . addslashes($row["color"]) . "',";
    echo "color_over: '" . addslashes($row["color_over"]) . "'";
    echo "},";
  echo "};";
} else {
  echo "No data found.";

Step 4: Integrate the generated settings into the map code

Include the "map_data.php" file in your HTML file, and replace the static map_data object with the dynamically generated one from the database.

<!-- start HTML5 Map -->
<div id='map-container'></div>
<link href="map.css" rel="stylesheet">
<script src="raphael.min.js"></script>
<script src="map.js"></script>
<script src="paths.js"></script>
<?php include 'map_data.php'; ?>
var map = new FlaMap(
  "mapWidth": "100%",
  "shadowAllow": false,
  // many other settings are available, please see the documentation
  "map_data": map_data
<!-- end HTML5 Map -->


Now you have successfully integrated an SQL database with the interactive map using PHP. The map settings are dynamically generated from the database, allowing for easy updates and maintenance. Don't forget to adjust your database connection settings and table/column names according to your specific database structure.

More tutorials


API reference