Ad:
 
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2707
  • PHP RSS
Similar Threads
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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
benhowdle89 is offline Offline
51 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: 153
Solved Threads: 229
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,398 posts
since May 2008
Jul 5th, 2010
0

Re: PHP check variable change

post your code.
Reputation Points: 10
Solved Threads: 7
Light Poster
raju_boini525 is offline Offline
44 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: 0
Junior Poster in Training
benhowdle89 is offline Offline
51 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: 153
Solved Threads: 229
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,398 posts
since May 2008
Jul 5th, 2010
0

Re: PHP check variable change

Brilliant, Thanks so much!!!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
benhowdle89 is offline Offline
51 posts
since Jun 2010
Jul 5th, 2010
0

Re: PHP check variable change

Always welcome! Please mark the thread as solved.
Reputation Points: 153
Solved Threads: 229
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,398 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.
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
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC