Count Data Records and delete old once - php

I'm working on a data monitor for solar panels.
Now i'm working on limited space, and getting a lot of data which is being logged on the database.
To downsize my space i was trying to find a way to make the datatables count everything every 15 minutes, and store it into a new table, and deleting the old tables.
I tried to do this with a cronjob, and later on tried to make a php script which would be handling it.
Now that code did not come close to what i was planning to have, and i'm stuck at this problem, and i know that there are probably people who do know the answer to this question.
I came across similar problems with searching through the site, but did not come across a "Count and Delete" question.
This to limit the space it uses.
Simply said, i'm trying to find a way with php to make it count and store the data records from "inverters" to "inverters_day", and deleting the excisting records from "inverters".
The Datatables are as following:
| timestamp | timestamp | No | CURRENT_TIMESTAMP
| inverter | int(11) | No |
| wh | int(11) | No |
| dcp | int(11) | No |
| dcc | float | No |
| efficiency | float | No |
| acf | int(11) | No |
| acv | float | No |
| temp | float | No |
| status | int(11) | No |
Example of data:
|2016-01-08 08:34:24|110134878|889901|0|0.05|0|49|55|2|1
|2016-01-08 08:34:59|110134878|889901|0|0.05|0|49|55|2|1
|2016-01-08 08:35:23|110048316|643076|0|0.05|0|49|55|1|1
Inverter_day is a duplication of the one above, structure it the same.
sorry, i forgot to add the code i tried.
if ($sql = mysqli_query ($conn, "SELECT COUNT (*) FROM logs WHERE timestamp < NOW() - INTERVAL 15 MINUTES")) {
$row_slt = mysqli_num_rows($sql);
if ($row_slt > 0 ) {
$sql = mysqli_query ($conn, "INSERT INTO inverter_day (timestamp, inverter, wh, dcp, dcc, efficiency, acf, acv, temp, status) SELECT timestamp, inverter, wh, dcp, dcc, efficiency, acf, acv, temp, status FROM logs WHERE timestamp NOT IN (select timestamp from inverter_day)");
} else if ($row_slt == 0) {
echo "<br> The Tables are up to date <br>";
} else {
echo "<br> Oops something went wrong. Please try again";
}
}

As i haven't had any further help with this, i just went on and left this part behind, untill i came across the INSERT SELECT feature.
The solution to the problem was the use of:
$query = "INSERT INTO enecsys_day (id, wh, dcpower, dccurrent, efficiency, acfreq, acvolt, temp, state) SELECT id, SUM(wh), SUM(dcpower), SUM(dccurrent), SUM(efficiency), SUM(acfreq), SUM(acvolt), SUM(temp), SUM(state) FROM enecsys GROUP BY id HAVING COUNT(id) > 1";
The full code i used:
<?php
mysql_connect($servername,$username,$password);
mysql_select_db($database);
$query = "INSERT INTO enecsys_day (id, wh, dcpower, dccurrent, efficiency, acfreq, acvolt, temp, state) SELECT id, SUM(wh), SUM(dcpower), SUM(dccurrent), SUM(efficiency), SUM(acfreq), SUM(acvolt), SUM(temp), SUM(state) FROM enecsys GROUP BY id HAVING COUNT(id) > 1";
$resultaat = mysql_query($query);
while ($row = mysql_fetch_array($resultaat))
{
?>
<tr>
<td><?php $row["id"]; ?></td>
<td><?php $row["SUM(wh)"]; ?></td>
</tr>
<br />
<?php
}
$delete = "DELETE FROM enecsys";
$dresultaat = mysql_query($delete);
?>

Related

I want to mark attendance of a student once per day in my database

I want to scan RFID cards from Arduino and it should mark attendance once per day not whenever it scans the card.
This is the PHP code for marking attendance:
public function getRFIDAttendanceApi(){
$rfid_uid = trim($_REQUEST['uid']);
$sql = "select count(*)as tot from tbl_users where rfid_uid='$rfid_uid'";
$data = $this->getData($sql);
if(count($data)>0 && $data[0]['tot']>0){
$sql = "insert into tbl_attendance set rfid_uid='$rfid_uid'";
$flag = $this->conn->query($sql);
if($flag){
echo "Attendance marked successfully!";
die;
} else {
echo "Sorry! Something unexpected happened!";
die;
}
} else {
echo "invalid access";die;
}
This is the table in mysql for attendance with repeating marking
| att_id | rfid_uid | punch_timestamp | date | time |
+--------+----------+---------------------+------------+----------+
| 1 | 85988145 | 2018-04-21 10:48:51 | 2018-04-21 | 10:48:51 |
| 2 | 85988145 | 2018-04-21 10:48:52 | 2018-04-21 | 10:48:52 |
How to make it once per day? Thanks.
Just add a unique contraint to the attendance table on the rfid_uid and date.
ALTER TABLE tbl_attendance ADD CONSTRAINT UK_ATTENTANCE UNIQUE(rfid_uid, `date`);
This way just the first successful punch on in a day from the card will be inserted in the database.
If your primary key (att_id, date) then you could not insert a given user with a given date more than once. Your application code then would handle the case in which a user try to register more than once a day, displaying an error message

Php/Mysql count dancers from each moment added issue

I have a dance contest site and each user can login and add dance moments,
in my html table with all moments from all users i have all the data but i want in a html column to add "number of dancers for each moment added by the logged user id".
I have this:
$c = mysql_query("SELECT * FROM moments");
$dancers = 0;
while($rows = mysql_fetch_array($c)){
for($i = 1; $i <= 24; $i++){
$dan_id = 'dancer'.$i;
if($rows[$dan_id] != "" || $rows[$dan_id] != null )
$dancers++;
}
}
echo "<th class="tg-amwm">NR of dancers</th>";
echo "<td class='tg-yw4l'>$dancers</td>";
phpMyAdmin moments table: has id, clubname, category, discipline, section, and this:
But this process is count all the dancers names from all users moments.
Example for this process: You have a total of 200 dancers !
I want the process to count for me all dancers names for each moment added in the form not a total of all entire users moments, something like this: if user john has two moments added: Moment 1: 5 dancers - moment 2: 10 dancers, and so on for each user.
Let me try to put you in the right way (it seems a long post but I think it's worth the beginners to read it!).
You have been told in the comments to normalize your database, and if I were you and if you want your project to work well for a long time... I'd do it.
There are many MySQL normalization tutorials, and you can google it your self if you are interested... I'm just going to help you with your particular example and I'm sure you will understand it.
Basically, you have to create different tables to store "different concepts", and then join it when you query the database.
In this case, I would create these tables:
categories, dance_clubs, users and dancers store "basic" data.
moments and moment_dancers store foreign keys to create relations between the data.
Let's see the content to understand it better.
mysql> select * from categories;
+----+---------------+
| id | name |
+----+---------------+
| 1 | Hip-hop/dance |
+----+---------------+
mysql> select * from dance_clubs;
+----+---------------+
| id | name |
+----+---------------+
| 1 | dance academy |
+----+---------------+
mysql> select * from users;
+----+-------+
| id | name |
+----+-------+
| 1 | alex |
+----+-------+
mysql> select * from dancers;
+----+-------+
| id | name |
+----+-------+
| 1 | alex |
| 2 | dan |
| 3 | mihai |
+----+-------+
mysql> select * from moments;
+----+--------------+---------------+-------------------+
| id | main_user_id | dance_club_id | dance_category_id |
+----+--------------+---------------+-------------------+
| 1 | 1 | 1 | 1 |
+----+--------------+---------------+-------------------+
(user alex) (dance acad..) (Hip-hop/dance)
mysql> select * from moment_dancers;
+----+-----------+-----------+
| id | moment_id | dancer_id |
+----+-----------+-----------+
| 1 | 1 | 1 | (moment 1, dancer alex)
| 2 | 1 | 2 | (moment 1, dancer dan)
| 3 | 1 | 3 | (moment 1, dancer mihai)
+----+-----------+-----------+
Ok! Now we want to make some queries from PHP.
We will use prepared statements instead of mysql_* queries as they said in the comments aswell.
The concept of prepared statement can be a bit hard to understand at first. Just read closely the code and look for some tutorials again ;)
Easy example to list the dancers (just to understand it):
// Your connection settings
$connData = ["localhost", "user", "pass", "dancers"];
$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]);
$conn->set_charset("utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Here we explain MySQL which will be the query
$stmt = $conn->prepare("select * from dancers");
// Here we explain PHP which variables will store the values of the two columns (row by row)
$stmt->bind_result($dancerId, $dancerName);
// Here we execute the query and store the result
$stmt->execute();
$stmt->store_result();
// Here we store the results of each row in our two PHP variables
while($stmt->fetch()){
// Now we can do whatever we want (store in array, echo, etc)
echo "<p>$dancerId - $dancerName</p>";
}
$stmt->close();
$conn->close();
Result in the browser:
Good! Now something a bit harder! List the moments:
// Your connection settings
$connData = ["localhost", "user", "pass", "dancers"];
$conn = new mysqli($connData[0], $connData[1], $connData[2], $connData[3]);
$conn->set_charset("utf8");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Query to read the "moments", but we have their main user and dancers in other tables
$stmtMoments = $conn->prepare("
select
moments.id,
(select name from users where users.id = moments.main_user_id) as main_user,
(select name from dance_clubs where dance_clubs.id = moments.dance_club_id) as dance_club,
(select name from categories where categories.id = moments.dance_category_id) as dance_category,
(select count(*) from moment_dancers where moment_dancers.moment_id = moments.id) as number_of_dancers
from moments
");
// Five columns, five variables... you know ;)
$stmtMoments->bind_result($momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers);
// Query to read the dancers of the "moment" with id $momentId
$stmtDancers = $conn->prepare("
select
dancers.name as dancer_name
from
dancers join moment_dancers on dancers.id = moment_dancers.dancer_id
where
moment_dancers.moment_id = ?
");
$stmtDancers->bind_param("i", $momentId);
$stmtDancers->bind_result($momentDancerName);
// Executing the "moments" query
$stmtMoments->execute();
$stmtMoments->store_result();
// We will enter once to the while because we have only one "moment" right now
while($stmtMoments->fetch()){
// Do whatever you want with $momentId, $momentMainUser, $momentDanceClub, $momentDanceCategory, $momentNumberOfDancers
// For example:
echo "<h3>Moment $momentId</h3>";
echo "<p>Main user: $momentMainUser</p>";
echo "<p>Dance club: $momentDanceClub</p>";
echo "<p>Category: $momentDanceCategory</p>";
echo "<p>Number of dancers: $momentNumberOfDancers</p>";
echo "<p><strong>Dancers</strong>: ";
// Now, for this moment, we look for its dancers
$stmtDancers->execute();
$stmtDancers->store_result();
while($stmtDancers->fetch()){
// Do whatever you want with each $momentDancerName
// For example, echo it:
echo $momentDancerName . " ";
}
echo "</p>";
echo "<hr>";
}
$stmtUsers->close();
$stmtMoments->close();
$conn->close();
Result in browser:
And that's all! Please ask me if you have any question!
(I could post the DDL code to create the database of the example with the content data if you want)
Edited: added dancers table. Renamed moment_users to moment_dancers. Changed functionality to adapt the script to new tables and names.

SQL query in PHP script keeps "losing" one row

Let's assume I have a table like this:
+------+---------+--------------+-------------+---------------+
| id | tstamp | product_id | name | configuration |
+------+---------+--------------+-------------+---------------+
| 3 | 15 | 02 | bike | abc |
| 2 | 16 | 03 | car | dfg |
| 1 | 11 | 04 | tree | ehg |
+------+---------+--------------+-------------+---------------+
When I run a simple query like
SELECT id, tstamp, product_id, name, configuration
FROM tl_iso_product_collection_item
ORDER BY `id` DESC
It returns 3 rows. As expected. Works, right? BUT, if I implement this query into a PHP script and try to fetch rows, theres always ONE missing. Always. Even in the most simple query. Where did I make a mistake?
Script looks like this:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$connection = mysqli_connect('localhost', 'user', 'pw', 'db');
mysqli_set_charset($connection,"utf8");
$query = "SELECT id, tstamp, product_id, name, configuration
FROM table
ORDER BY `id` DESC "
$result = mysqli_query($connection, $query);
while ($row=mysqli_fetch_row($result)) {
echo $row[0];
echo $row[1];
echo $row[2];
echo $row[3];
echo $row[4];
}
mysqli_close($connection);
?>
This will result in displaying only 2 out of 3 rows. My question is, why tho? If I insert the query above directly into phpmyadmin it shows 3 results, not 2. What is wrong here? Am I missing something? Thanks in advance.
$fetch_allgemein = mysqli_fetch_array($result_allgemein);
$fetch_configuration = mysqli_fetch_array($result_configuration);
You are fetching the first record for each query result here already – so the internal “pointer” to the current record gets advanced to the second record in each result set, and therefor when you loop over the result using
while ($row=mysqli_fetch_row($result_allgemein)) {
later on, the first record is of course omitted.
You will need to reset the “pointer” using mysqli_result::data_seek if you want to get all records in your loops after you’ve already fetched the first record each.

How to increment the search count in php/mysql?

I have a table consists of several fields (id, firstname, surname, username, search_count)
I've build a small search engine that search my table to find any match exists either in the firstname or in the surname and I am getting the results with no problems.
Now, what I am trying to do is to increment the search_count field by 1 every time there is a match!
For example let's say we have the following table users:
id | firstname | surname | username | search_count
1 | John | Mike | un1 | 0
2 | John | Jeff | un2 | 0
3 | Dale | John | un3 | 0
4 | Mike | Gorge | un4 | 0
and let's say we are searching for Jeff as a keyword
so, the query will return 1 record
what I want to do is to increment the search_count by 1 for match record
so the results will be something like as:
id | firstname | surname | username | search_count
2 | John | Jeff | un2 | `1`
and if we make a new search (e.g. John) the results should be something like:
id | firstname | surname | username | search_count
1 | John | Mike | un1 | 1
2 | John | Jeff | un2 | 2
3 | Dale | John | un3 | 1
I've tried several approach but with no luck.. So I appreciate any hints and help
here is my code...
<?php
// open the HTML page
include 'html_open.php';
// require the db connection
require '/inc/db.inc.php';
// require the error messages
require '/inc/echo.inc.php';
if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
if (!empty($keyword)) {
// build our search query
$search_query = "SELECT * FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
// run the search query
$search_query_run = mysql_query($search_query);
// search results
$search_results_num = mysql_num_rows($search_query_run);
// check query return results
if ($search_results_num>0) {
echo 'Search engine returns <strong>[ '.$search_results_num.' ]</strong> result(s) for <strong>[ '.$keyword.' ]</strong>:<br>';
// retrieving the information found
echo '<ol>';
while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
//$current_search_count = ;
echo '<li>'.$search_result_information['username'].'. This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
}
echo '</ol><hr>';
include 'search_form.php';
} else {
echo '<hr>Search engine returns no result for <strong>[ '.$keyword.' ]</strong>, please try another keyword.<hr>'; // hint: no result found
include 'search_form.php';
}
} else {
echo $err20_002; // hint: must insert input
include 'search_form.php';
}
} else {
echo $err20_001; // hint: form has not been submitted
include 'search_form.php';
}
// close the HTML page
include 'html_close.php';
?>
P.S. I am new to PHP / MySQL and this is my first code :)
...
while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
# add this following line
mysql_query('UPDATE `users` SET search_count=search_count+1 WHERE id='.$search_result_information['id']);
# Edit. Change result to show new number
$search_result_information['search_count']++; # this adds 1 to the value (does not affect stored data in database)
echo '<li>'.$search_result_information['username'].'. This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
}
...
In your case you need to fetch the found values and perform an update statement adding +1 to the search_count column
$search_query = "SELECT id, firstname, surname, username, search_count FROM `users` WHERE `firstname` = '".mysql_real_escape_string($keyword)."' OR `surname` = '".mysql_real_escape_string($keyword)."' ORDER BY `search_count` DESC";
$search_query_run = mysql_query($search_query);
// search results
$search_results_num = mysql_num_rows($search_query_run);
// check query return results
if ($search_results_num>0) {
echo 'Search engine returns <strong>[ '.$search_results_num.' ]</strong> result(s) for <strong>[ '.$keyword.' ]</strong>:<br>';
// retrieving the information found
echo '<ol>';
while ($search_result_information = mysql_fetch_assoc($search_query_run)) {
//$current_search_count = ;
$update_search = "UPDATE users SET search_count = search_count + 1 WHERE id = {$search_result_information['id']}"; // so every `id` will increment its search_count with 1. You will need to select the rows once again, to take this count, or to manually increment in PHP
mysql_query($update_search);
echo '<li>'.$search_result_information['username'].'. This user has been searched: '.$search_result_information['search_count'].' times before.</li>';
P.S.: Using mysql_* lib is strongly NOT recommended. As you can see from the red box in the official documentation http://www.php.net/manual/en/function.mysql-query.php you should choose one of the current actually supported APIs

What is the correct way to join two tables in SQL?

I have two tables. The first table holds simple user data and has the columns
$username, $text, $image
(this is called "USERDATA").
The second table holds information about which users "follow" other users, which is set up with the columns
$username and $usertheyfollow
(this is called "FOLLOWS").
What I need to do is display the data individually to each user so that it is relevant to them. This means that userABC for instance, needs to be able to view the $text and $image inputs for all of the users whom he/she follows. To do this, I believe I need to write a sql query that involves first checking who the logged in user is (in this case userABC), then selecting all instances of $usertheyfollow on table FOLLOWS that has the corresponding value of "userABC." I then need to go back to my USERDATA table and select $text and $image that has a corresponding value of $usertheyfollow. Then I can just display this using echo command or the like...
How would I write this SQL query? And am I even going about the database architecture the right way?
With tables like so:
userdata table
______________________________
| id | username | text | image |
|------------------------------|
| 1 | jam | text | image |
+------------------------------+
| 2 | sarah | text | image |
+------------------------------+
| 3 | tom | text | image |
+------------------------------+
follows table
_____________________
| userId | userFollow |
|---------------------|
| 1 | 2 |
+---------------------+
| 1 | 3 |
+---------------------+
and use the following SQL:
SELECT userdata.text, userdata.image FROM follows LEFT JOIN userdata ON follows.userFollow = userdata.id WHERE follows.userId = 1
will get all the text and images that user with id '1' follows
As it turns out, neither of these answers were right. #jam6459 was closest.
The correct answer is the following:
SELECT userdata.text, userdata.image, follows.userFollow
FROM userdata
LEFT JOIN follows ON follows.userFollow = userdata.username
WHERE follows.userId = $username
I also found it easier to not have a username correspond to an Id as in jam's table example. This is because the same user can have multiple entries in "USERDATA". I instead used username as the Id.
function get_text_image($username)
{
$sql = "SELECT * FROM USERDATA where username='".$username."'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo $row['text'];
echo $row['image'];
}
}
function display_data_of_followers($userid)
{
$sql = "SELECT usertheyfollow FROM follow WHERE userid = ".$userid."";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
get_text_image($row['usertheyfollow']);
}
}
display_data_of_followers($userid);

Categories