使用php文件中的查询生成的值

时间:2022-07-01 01:51:05

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

在一个php文件(有效)我​​从数据库生成一个值(最后记录的温度)

<?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]

但是在另一个php文件中,我必须在一个现在有固定值的地方使用这个值:[80]

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

如何用第一个php文件生成的值替换该值80?

2 个解决方案

#1


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;
   }
}

#2


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.

如果你调用现有的脚本latest_temperature.php,它只打印最新的temp,我们可以从另一个脚本调用它。

another.php

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.

#1


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;
   }
}

#2


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.

如果你调用现有的脚本latest_temperature.php,它只打印最新的temp,我们可以从另一个脚本调用它。

another.php

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.