954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP check variable change

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

benhowdle89
Junior Poster in Training
81 posts since Jun 2010
Reputation Points: 10
Solved Threads: 2
 

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.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

post your code.

raju_boini525
Junior Poster in Training
53 posts since Aug 2009
Reputation Points: 10
Solved Threads: 7
 

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

benhowdle89
Junior Poster in Training
81 posts since Jun 2010
Reputation Points: 10
Solved Threads: 2
 
<?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";
?>
rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

Brilliant, Thanks so much!!!

benhowdle89
Junior Poster in Training
81 posts since Jun 2010
Reputation Points: 10
Solved Threads: 2
 

Always welcome! Please mark the thread as solved.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You