942,528 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 3558
  • PHP RSS
Jul 5th, 2010
0

PHP check variable change

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
benhowdle89 is offline Offline
81 posts
since Jun 2010
Jul 5th, 2010
0
Re: PHP check variable change
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.
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jul 5th, 2010
0
Re: PHP check variable change
post your code.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
raju_boini525 is offline Offline
53 posts
since Aug 2009
Jul 5th, 2010
0
Re: PHP check variable change
That sounds perfect, would you mind putting a bit of sample code together please?

This is my code so far:

PHP Syntax (Toggle Plain Text)
  1. <?php
  2. header('refresh:30; url=pachube.php');
  3. require_once( '/pachube_php_library/pachube_functions.php' );
  4. $api_key = "xxxxxxxxxxxxxxxxx";
  5. $pachube = new Pachube($api_key);
  6. $feed = xxxxx;
  7. $data = $pachube->retrieveData ( $feed, "csv" );
  8. $array = explode(',', $data, 5);
  9. $value = $array[0];
  10. echo $value;
  11.  
  12. //Connect to database
  13. $opendb = mysql_connect("localhost", "root", "") or mysql_error("Could not connect to database");
  14. mysql_select_db("test");
  15.  
  16. if ($opendb)
  17. {
  18. mysql_query(" INSERT INTO arduino (data) VALUES ($value)");
  19. mysql_close($opendb);
  20. }
  21.  
  22. ?>

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

Thanks
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
benhowdle89 is offline Offline
81 posts
since Jun 2010
Jul 5th, 2010
0
Re: PHP check variable change
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. header('refresh:30; url=pachube.php');
  3. require_once( '/pachube_php_library/pachube_functions.php' );
  4. $api_key = "xxxxxxxxxxxxxxxxx";
  5. $pachube = new Pachube($api_key);
  6. $feed = xxxxx;
  7. $data = $pachube->retrieveData ( $feed, "csv" );
  8. $array = explode(',', $data, 5);
  9. $value = $array[0];
  10. echo $value;
  11. if (isset($value) && !empty($value) && $value != NULL)
  12. {
  13. //Connect to database
  14. $opendb = mysql_connect("localhost", "root", "") or mysql_error("Could not connect to database");
  15. mysql_select_db("test");
  16. if ($opendb)
  17. {
  18. mysql_query(" INSERT INTO arduino (data) VALUES ($value)");
  19. mysql_close($opendb);
  20. }
  21. $value = '';
  22. } else echo "No value is set";
  23. ?>
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jul 5th, 2010
0
Re: PHP check variable change
Brilliant, Thanks so much!!!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
benhowdle89 is offline Offline
81 posts
since Jun 2010
Jul 5th, 2010
0
Re: PHP check variable change
Always welcome! Please mark the thread as solved.
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: why can not I upload fails more than 1.42 MB?
Next Thread in PHP Forum Timeline: Need Litle Help MYSQL & PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC