Exploring SQL Components with JavaScript

Here is the code I am currently using:

//This function handles all games and their attributes
function handleGames(){
    sql.query('SELECT id FROM games', function (err, rows){
        if(err){
            console.log(String(err).error.bgWhite);
        } else {
            for(var i = 0; i < rows.length; i++) {

                var timeNow = new Date().getTime();

                //Game Expiration
                //0 = Open | 4 = Expired | 5 = Generic Error

                //Generic error --> Expired
                if(rows[i]["status"] == 5){
                    sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){
                        if(err) console.log(String(err).error.bgWhite);
                        console.log(("Updating status for game " + rows[i]["id"] + " to 4").info.bgWhite);
                    });
                }

                //Set game status to expired if it has lasted more than 5 minutes (300s)
                if(((rows[i]["starttime"] + 300) >= timeNow) && (rows[i]["status"] == 0)){
                    sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){
                        if(err) console.log(String(err).error.bgWhite);
                    });
                }
            };
        }
    });
}

Using this data:

https://i.stack.imgur.com/1NWxm.png

This program is designed for Node and aims to update the status of any game that has been active for 5 minutes (300s), but there seems to be an issue with how the keys are accessed. The game status remains unchanged.

JSON Version


SQL Export:

-- phpMyAdmin SQL Dump
...
... (rest of the SQL export content)
...

Answer №1

Calculate the time duration in seconds by using

((new Date()).getTime()-starttime)/1000
. Feel free to share your code on an online JavaScript editor like Plunker! Are you here?

INSERT INTO `games` (`id`, `starttime`, `status`, `ct_name`, `ct_avatar`, `ct_steamid`, `ct_bet`, `t_name`, `t_avatar`, `t_steamid`, `t_bet`) VALUES 
(3, 1461862619, 0, 'Robinlemon > CSGOVoid.net', 'steamcdn-a.akamaihd.net/…', 76561198065346589, 3, 'Yuuta', 'cdn.akamai.steamstatic.com/…', 76561198052096214, 3);

This data is included in your SQL dump! It inserts an entity with ID (using AUTO_INCREMENT) as 3!! For subsequent inserts, AUTO_INCREMENT will be set as 4!!!

If you wish to change that, you can delete the row from the table and reset the AUTO_INCREMENT counter.

ALTER TABLE tablename AUTO_INCREMENT = 1

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Extract the "_id" value from the array in the v-for loop and then store and reuse it in the data object // using vue-cli

Currently, I am iterating through [postArray] to retrieve various posts, each of which has its own unique "_id". My goal is to utilize this "_id" to add likes to the corresponding post. This is the v-for loop I am using: <div class="posts" v- ...

Feeling puzzled about the next() function in Node.js?

https://github.com/hwz/chirp/blob/master/module-5/completed/routes/api.js function isAuthenticated (req, res, next) { // If the user is authenticated in the session, call the next() to proceed to the next request handler // Passport adds this met ...

Maintain checkbox selection even after leaving the page

I am looking for a way to maintain the state of a checkbox even after navigating away from the page in an AngularJS and bootstrap environment. Currently, I find that my bound variable gets reset to false every time I reload the page when the controller run ...

I need help with installing npm packages on my Linux operating system - can someone provide

I've recently set up nodejs v7.5.0 and npm v4.1.2 on my 32-bit Linux Mint system. When I try to run npm install in the terminal, instead of installing npm packages, I receive the following information: npm WARN optional SKIPPING OPTIONAL DEPENDENCY: ...

socket.io establishes several sockets for each connection

At times, when the server is experiencing some load, connecting to the page may result in multiple sockets being created. If there is significant lag, the connection may never be established while additional sockets are generated every second indefinitely. ...

Navigational paths for the persistent sidebar in Material UI

I am looking to utilize this as a page navigation, but I am struggling with properly linking it to the index. I have attempted separating the items, but that approach did not work as intended. <List> {['Home' , 'Abo ...

The scroll feature in JavaScript is malfunctioning

After countless hours of troubleshooting, I still can't figure out why the code snippet below is not working properly on my website at : <script> $(window).scroll(function () { if ($(window).scrollTop() > 400) { ...

Modifying the color of a non-active tab - Material UI Tabs

Is there a way to customize the color of inactive tabs in Material UI tabs? I have noticed that they currently appear black, as shown in the screenshot here: screenshot Thank you! ...

Verify if the value present in the array exists and is less than the succeeding value within the same array

In the task at hand, I am tasked with verifying whether a value entered into an input field exists in an array and is smaller than the subsequent value in the same array. The purpose of this check is to adjust the quantity of items in a shopping cart. Cor ...

Limiting the length of parameters in an Angular directive

Is there a character limit for the parameter being sent to this directive? I'm encountering an issue with my code: header = JSON.stringify(header); columnObj = JSON.stringify(columnObj); $compile('<div column-filter-sort header=' + heade ...

Can you provide me the steps to delete the title attribute from images in Wordpress?

My client has expressed dissatisfaction with the tooltip that appears when hovering over images in certain browsers, particularly Safari. This tooltip displays the title attribute within the img tag, which is a requirement enforced by Wordpress. Even if w ...

Reconfigure an ancestral item into a designated key

I have a single object with an array of roles inside, and I need to transform the roles into an array of objects. See example below: Current Object: displayConfiguration: { widgetList: { widgetName: 'widget title', entityType: 'As ...

Is there a way to populate fields on a hidden input using Mechanize?

When I try to use the form to navigate to a different page, I realize that the input field is hidden. Below is the HTML code for reference: <form id="form_pager" method="post" action=""> <input type="hidden" id="txtPage" name="page"> ...

Troubleshooting the issue of a callback function not properly updating state within the componentDidMount

I am currently utilizing Next.js and have the following functions implemented: componentDidMount = () => { //Retrieves cart from storage let self = this this.updateCart(Store.getCart(), self) ... } updateCart = (cart, self) => { ...

Assistance in changing an onClick function in JavaScript to onLoad

I've been working on updating a JavaScript function to trigger both on page load and window resize instead of just a click event. In the code snippet below, I've made adjustments by commenting out the section related to the click event, added "wi ...

Setting the default value in a Reactive form on the fly: A step-by-step guide

When creating a table using looping, I need to set the default value of my Reactive Form to `Repeat` if the loop value matches a particular character, otherwise I want it to be empty. Here is my code: typescript rDefault:string = ""; create(){ ...

Transferring Data from One MySQL Table to Another

Is there a way to randomly select an address from one table and insert it into a column in another table? I keep receiving an error message stating "no database selected." if ($_SESSION[address] == "") { $db = @mysql_select_db($db_bitcoins,$connection ...

Should version numbers in package.json be specified precisely?

Traditionally, developers have entered exact version numbers (e.g., 1.2.4) for npm dependencies in package.json rather than inexact version numbers (e.g., ^1.2.4), which allows for bug fix releases such as 1.2.5. This practice aims to ensure that future in ...

How can I declaratively bind the properties in Dojo's _hasDropDown method?

UniqueSearchComponent.html: <div class="${baseClass}"> <div data-dojo-type="dijit/_HasDropDown" data-dojo-props="dropDown: 'containerNode'"> <div data-dojo-type="dijit/form/TextBox" name="${SearchViewFieldName} ...

Tips for selecting a JSON data node on-the-fly using jQuery

This is an example of my ajax function: $.ajax({ type: "GET", dataType: "json", async: false, url: "/wp-content/comment_data.php", data: 'songid=' + $array, success: function(data){ oTable.find('td').eac ...