I am having a problem with my PHP code. I have a series of columns in MySQL database, PotVal, Current, ID, kWh and costKwh. Where ID is AUTO_INCREMENT, PotVal and Current are from a Text file - these are working correctly and storing the data fine. However i would like to take PotVal which is actually sensed power and convert this to KiloWatt hours using (PotVal*1)/(900) (don't worry I know its 1000 normally not 900) - this is also working fine and outputting the data in realtime, however I am trying to store this value in my kWh colum. Next I use this kWh column to carry out another calculation for cost per kWh using (kWh*18.56). However due to not storing data in kWh column I am getting 0.00 output and I can't rectify it!
So in summary why am I not able to store in kWh? And also I want to display the value to two decimal places but its actually displaying to six decimal places!
<?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);
?>
(I bet it's something simple... again!)
$perkWh = 'SELECT *, (PotVal/900) AS kWh
FROM received_data ORDER BY ID DESC LIMIT 1';
I think you already have a kWh column in received_data table, so you can not naming (PotVal/900) as kWh
same problem in $cperkWh = 'SELECT *, (kWh*18.67) AS costkWh
FROM received_data ORDER BY ID DESC LIMIT 1';
with costkWh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With