我的php代码有问题。我在mysql数据库中有一系列的列,potVal,current,id,kWh和costkWh。其中id是auto_increment,potVal和current来自一个文本文件-它们工作正常并且存储数据良好。不过,我想取实际感测功率的potVal,并使用(potVal*1)/(900)将其转换为千瓦时(不用担心,我知道它通常是1000而不是900)-这也很好,可以实时输出数据,但是我试图将此值存储在我的kWh列中。接下来,我使用这个kWh列对每kWh使用的成本进行另一个计算(kWh*18.56)。但是由于没有在kWh列中存储数据,我得到了0.00的输出,我无法纠正它!
总而言之,为什么我不能以kWh为单位存储?我想把这个值显示到小数点后两位,但实际上它显示到了小数点后六位!
<?php
header('Refresh: 1.5');
// Set relevent pseudo-variables
$hostname = 'localhost';
$username = 'root';
$password = '';
$table = 'received_data';
$drop = 'DROP TABLE received_data';
$iwemsdatafile = 'IWEMSData.txt';
// Connect to MySQL
$dbconnect = mysql_connect($hostname, $username, $password)
or die('Unable to connect to MySQL: ' . mysql_error());
// echo 'Connected to MySQL<br>';
mysql_query($drop);
// Create database, table and columns
$cdb = 'CREATE DATABASE IWEMS_Data';
// echo 'IWEMS_Data has been created<br /><hr />';
$selectdb = mysql_select_db('IWEMSData',$dbconnect)
or die('Could not select IWEMSData');
// echo 'Connected to IWEMSData database<br />';
mysql_query($drop);
$ctb = 'CREATE TABLE received_data
(
Current DECIMAL(30,2) NOT NULL,
PotVal DECIMAL(30,2) NOT NULL,
kWh DECIMAL(30,2) NOT NULL,
costkWh DECIMAL(30,2) NOT NULL,
ID BIGINT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID,PotVal,Current)
)';
mysql_query($ctb, $dbconnect);
// echo 'Received_Data Table has been created<br />';
// echo 'Current, PotVal and ID columns have been created<br /><hr />';
// Push IWEMSData.txt data into MySQL database
mysql_query
('
LOAD DATA LOCAL INFILE "IWEMSData.txt"
REPLACE INTO TABLE received_data
FIELDS TERMINATED BY " "
LINES TERMINATED BY "\\r\\n"
(PotVal, Current)
;')
or die('Error Loading Data File.<br>' . mysql_error());
// Pull IWEMSData from MySQL database
$seldat = 'SELECT * FROM received_data ORDER BY ID DESC LIMIT 1';
$data = mysql_query($seldat)
or die('Error retrieving data: ' . mysql_error());
// Display data on web browser
while ($finalrow = mysql_fetch_array($data))
{
echo '<hr /> Apparent Power<br />';
echo $finalrow['PotVal'];
echo ' Watts';
echo '<br /><hr />';
echo 'Detected Current<br />';
echo $finalrow['Current'];
echo ' Amps';
echo '<br /><hr />';
}
// Display per kWh data on web browser
$perkWh = 'SELECT *, (PotVal/900) AS kWh
FROM received_data ORDER BY ID DESC LIMIT 1';
$qperkWh =mysql_query($perkWh) or die('Error');
while ($finalkWh = mysql_fetch_array($qperkWh))
{
echo 'kWh<br />';
echo $finalkWh['kWh'];
echo '<br /><hr />';
}
// Display cost per kWh data on web browser
$cperkWh = 'SELECT *, (kWh*18.67) AS costkWh
FROM received_data ORDER BY ID DESC LIMIT 1';
$cqperkWh =mysql_query($cperkWh) or die('Error');
while ($cfinalkWh = mysql_fetch_array($cqperkWh))
{
echo 'Cost kWh<br />';
echo $cfinalkWh['costkWh'];
echo '<br /><hr />';
}
// Close Database connection when complete
mysql_close($dbconnect);
?>
(我打赌这很简单……再来一次!
最佳答案
$perkWh = 'SELECT *, (PotVal/900) AS kWh
FROM received_data ORDER BY ID DESC LIMIT 1';
我认为您已经在
kWh
表中有一个received_data
列,因此不能将(PotVal/900)
命名为kWh
$cperkWh = 'SELECT *, (kWh*18.67) AS costkWh FROM received_data ORDER BY ID DESC LIMIT 1';
与costkWh
中的相同问题