# Need help with storing event variable in a database connected to website

**URL:** <https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925>\
**Category:** General\
**Created:** [June 20, 2017, 11:09am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925 "2017-06-20T11:09:13Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucafro](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Lucafro](https://community.particle.io/u/Lucafro)\
**Post date:** [June 20, 2017, 11:09am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/1 "2017-06-20T11:09:13Z")

</div>

Hello everyone,  
I am working on a university project at the moment where we have an exhibition of an interactive art installation that will be placed in my local city for 3 days. On our [website](http://flourish.bplaced.net/) about the art installation we want to implement a counter that simply counts the amounts of interactions that people have done with our installation in these 3 days. The signal that an interaction has taken place would come from the Arduino which is connected to the Photon.

I have done a [prototype](https://www.youtube.com/watch?v=77YxsvZzKzQ) of this where I used a simple pushbutton that substitutes the signal coming from the Arduino.

The html/js code for this:

```
<script type="text/javascript">     
function start() {

  // document.getElementById("flower").innerHTML = "Waiting for data...";
    var deviceID = "CENSORED";
    var accessToken = "CENSORED";
    var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

    eventSource.addEventListener('open', function(e) {
        console.log("Opened!"); },false);

    eventSource.addEventListener('error', function(e) {
        console.log("Errored!"); },false);

    eventSource.addEventListener('+1', function(e) {
        
        //console.log("received '" + e-data + "'" );
        var parsedData = JSON.parse(e.data);
        var flowerSpan = document.getElementById("flower");
        var tsSpan = document.getElementById("tstamp");
        localStorage.flowercount = Number(localStorage.flowercount)+1;
        //tempSpan.innerHTML = "Core: " + parsedData.coreid + " flower: " + parsedData.data + " (h:m:s)";
        //tempSpan.innerHTML = "flower";
        flowerSpan.innerHTML = "There have been " + localStorage.flowercount + " interactions with the flower.";
        flowerSpan.style.fontSize = "28px";
        tsSpan.innerHTML = "At timestamp " + parsedData.published_at;
        tsSpan.style.fontSize = "9px";
    }, false);
}

```

`

This works, the problem is that it stores the counter variable only locally in the browser.  
What I actually need is storing the counter number somewhere on a web server and then display the number on my project website, where it gets updated regulary.

I’ve already researched a lot and tried to work with a MySQL database, unfortunately I’m lacking the experience and skills in these fields.  
I’ve also followed [this](https://community.particle.io/t/using-webhooks-and-webtasks-to-send-particle-data-to-a-database/25631) tutorial, as it looks like something that I need. Unfortunately it gives me a hook error in the console.particle event log. I can’t seem to find where the error is so I am looking for a different(possibly easier) solution for my problem.

At the moment I am quite desperate and any help or tips are appreciated!

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 20, 2017, 11:37am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/2 "2017-06-20T11:37:55Z")

</div>

Just a thought, why not store the number on the Photon and publish and expose as `Particle.variable()`?

---

<div class="post-metadata">

**Author:** ![Lucafro](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Lucafro](https://community.particle.io/u/Lucafro)\
**Post date:** [June 20, 2017, 11:40am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/3 "2017-06-20T11:40:11Z")

</div>

Thanks for your reply. I thought about that but figured that the number would probably be erased in case the Photon turns off or loses connection to the WiFi. Do you think there is a way to keep the number stored even if the Photon turns off or loses connection?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [June 20, 2017, 11:44am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/4 "2017-06-20T11:44:49Z")

</div>

If you use [`retained`](https://docs.particle.io/reference/firmware/photon/#backup-ram-sram-) variables the data won’t be lost after a reset (and mere cloud loss doesn’t erase any variables).  
Retained variables are stored in a special RAM area that’ll be held valid as long the device is powered, and to gain independency from the grid power you can add a coin cell to provide backup power to VBAT.  
And if that seems too insecure, you could even use [EEPROM](https://docs.particle.io/reference/firmware/photon/#eeprom).

---

<div class="post-metadata">

**Author:** ![Lucafro](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Lucafro](https://community.particle.io/u/Lucafro)\
**Post date:** [June 20, 2017, 12:16pm UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/5 "2017-06-20T12:16:22Z")

</div>

Thank you, this is a great idea. I just talked to my teachers again and unfortunately I HAVE to use a database in this project… So I still need to do it in the complicated database way

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [June 20, 2017, 12:53pm UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/6 "2017-06-20T12:53:36Z")

</div>

Does firebase database qualify?

---

<div class="post-metadata">

**Author:** ![Lucafro](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Lucafro](https://community.particle.io/u/Lucafro)\
**Post date:** [June 20, 2017, 1:00pm UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/7 "2017-06-20T13:00:32Z")

</div>

I gave it a look and I think it would qualify as a database, yes.

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [June 20, 2017, 1:01pm UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/8 "2017-06-20T13:01:07Z")

</div>

This tutorial should be handy:

> [@Google Firebase with webhooks tutorial](https://community.particle.io/t/google-firebase-with-webhooks-tutorial/26323):
>
> I created a new tutorial for using Google Firebase and webhooks for storing data from Photons, Electrons, etc… There are three examples in the tutorial: Saving data in a table Saving data in a per-device table Saving data from a device and retrieving it from another device You don’t need your own server; it’s all done with webhooks and the regular Google Firebase API. And if you don’t have much data to exchange, you may be able to use the free tier forever.

---

<div class="post-metadata">

**Author:** ![RWB](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/rwb/32/22599_2.png) [@RWB](https://community.particle.io/u/RWB)\
**Post date:** [June 20, 2017, 4:49pm UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/9 "2017-06-20T16:49:16Z")

</div>

Ubidots can also be used to database the data sent out from a Photon or Electron.

It’s probably easier to setup than the Firebase solution also.

Just another option.

---

<div class="post-metadata">

**Author:** ![vibrocil](https://avatars.discourse-cdn.com/v4/letter/v/71c47a/32.png) [@vibrocil](https://community.particle.io/u/vibrocil)\
**Post date:** [June 21, 2017, 1:53am UTC](https://community.particle.io/t/need-help-with-storing-event-variable-in-a-database-connected-to-website/33925/10 "2017-06-21T01:53:43Z")

</div>

Call a php script that adds the data to the mysql database. If you have a mysql database with phpmyadmin you just create a table with a column autoID and another field (string) for data or whatever. Than call a simple phpscript that inserts some data into the table. Script in php would look something like this:

```
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyTable (youdatacolumn)
VALUES ('".$_POST['yourdata']."')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

```

Taken from w3schools

Assuming you got jquery in your code you call the script with:

```
$.ajax({
    data: 'yourdata=' + yourdata,
    url: 'your.php',
    method: 'POST',
    success: function(msg) {
        alert(msg);
    }
});

```

You can edit according to your needs, like no alert on succes.
