我的PHP代码有问题。我在MySQL数据库中有一系列专栏,PotVal,Current,ID,kWh和costKwh。其中ID为AUTO_INCREMENT,PotVal和Current来自文本文件 - 这些工作正常并且存储数据正常。但是我想拿PotVal实际感测到的功率并使用(PotVal * 1)/(900)将其转换为KiloWatt小时(不要担心我知道它的1000通常不是900) - 这也工作正常并输出实时数据,但我试图将此值存储在我的kWh列中。接下来,我使用这个kWh列进行另一个每千瓦时成本的计算(kWh * 18.56)。但是由于没有以kWh列存储数据,我得到0.00输出,我无法纠正它!
总结为什么我无法以千瓦时存储?而且我想将值显示为两位小数,但实际显示为六位小数!
<?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);
?>
(我打赌这很简单......再次!)
答案 0 :(得分:2)
$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
答案 1 :(得分:1)
我看到你的查询中有大写字母。当我使用这种类型的查询时,我记得使用大写查询时遇到了一些奇怪的问题。所以我真的建议你不要使用大写字母,只使用小写字母。
如果你想使用大写字母,请务必将所有数据库,列,表名称括在引号中,我的意思是使用
`kWh`
并检查问题是否仍然存在。这可能不是问题,但可能是它。 :)
并且建议不要使用可能被弃用的mysql*
函数,并且它们会遇到许多安全问题,例如Injections。请改用PDO
。你可以在PHP文档中看到doc。