0

In a php file (that works) I generate one value from a database (being the last logged temperature)

<?php
$con = mysql_connect("localhost","datalogger","datalogger");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("datalogger", $con);

$result = mysql_query("SELECT * FROM datalogger.datalogger order by      date_time desc limit 1");

while($row = mysql_fetch_array($result)) {
  echo $row['temperature']. "\n";
}

mysql_close($con);
?>

But in another php file I have to use this value at a place where there is now a fixed value value: [80]

How do I replace that value 80 with the value generated by the first php file ?

SKALIS
  • 3
  • 5
  • 1
    you need to elaborate on your question, I can't make heads or tails out of it (after re-reading it over and over again). and show us what your other file holds – Funk Forty Niner Dec 09 '15 at 22:22
  • Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Dec 09 '15 at 22:23

2 Answers2

0

It sounds like you need a helper function or class.

function get_latest_temperature()
{
    // Your db call
    return $temperature;
}

Helper class:

class TemperatureModel
{
   private $db_conn;

   public function __construct($db_conn)
   {
       $this->db_conn = $db_conn;
   }
   public function getLatestTemperature()
   {
       // Your db call
       return $temperature;
   }
   // Fictitious method
   public function getLowestEverTemperature()
   {
       // Another db call
       return $temperature;
   }
}
Progrock
  • 7,373
  • 1
  • 19
  • 25
0

Quick way:

If you call your existing script latest_temperature.php, which just prints the latest temp, we could call that from another script.

another.php

<?php

$latest_temp = file_get_contents('http://example.com/path/to/latest_temperature.php');

// OR

$latest_temp_func = function() {
    ob_start();
    include_once 'latest_temperature.php';
    return ob_get_clean();
};
$latest_temp = $latest_temp_func();

// Begin output
?>
The latest temperature is <?php echo $latest_temp; ?> degrees.
Progrock
  • 7,373
  • 1
  • 19
  • 25