Hi,

What i have is a variable that is getting updated periodically and i've set the php script to refresh itself every 30 seconds to run a mysql query updating a db, this is good but if this value isnt changing from the source i get duplicate records in the db, how do i check the value of the variable with php and only execute the query when the variable has changed value?

Thanks

Recommended Answers

Before generating the next value set your variable to clear or NULL after updating the db, and before updating check whether variable is clear or NULL skip the update process.

Jump to Post

post your code.

Jump to Post

All 6 Replies

Member Avatar

Before generating the next value set your variable to clear or NULL after updating the db, and before updating check whether variable is clear or NULL skip the update process.

post your code.

That sounds perfect, would you mind putting a bit of sample code together please?

This is my code so far:

<?php
header('refresh:30; url=pachube.php');
require_once( '/pachube_php_library/pachube_functions.php' );
$api_key = "xxxxxxxxxxxxxxxxx";
$pachube = new Pachube($api_key);
$feed = xxxxx;
$data = $pachube->retrieveData ( $feed, "csv" );
$array = explode(',', $data, 5);
$value = $array[0];
echo $value;

	//Connect to database
	$opendb = mysql_connect("localhost", "root", "") or mysql_error("Could not connect to database");
    mysql_select_db("test");

    if ($opendb)
		{
     	mysql_query(" INSERT INTO arduino (data) VALUES ($value)");
		mysql_close($opendb);
		}
		
?>

So i'm guessing i would need to put some sort of "if" statement above the mysql query??

Thanks

Member Avatar
<?php
header('refresh:30; url=pachube.php');
require_once( '/pachube_php_library/pachube_functions.php' );
$api_key = "xxxxxxxxxxxxxxxxx";
$pachube = new Pachube($api_key);
$feed = xxxxx;
$data = $pachube->retrieveData ( $feed, "csv" );
$array = explode(',', $data, 5);
$value = $array[0];
echo $value;
if (isset($value) && !empty($value) && $value != NULL)
{
	//Connect to database
	$opendb = mysql_connect("localhost", "root", "") or mysql_error("Could not connect to database");
    mysql_select_db("test");
    if ($opendb)
		{
     	mysql_query(" INSERT INTO arduino (data) VALUES ($value)");
		mysql_close($opendb);
		}
	$value = '';
} else echo "No value is set";
?>

Brilliant, Thanks so much!!!

Member Avatar

Always welcome! Please mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.