samedi 14 décembre 2019

Query "SELECT *..." not returning actual values

I'm making a little command-line database dashboard just to learn. I made this same dashboard with php and c# and they both work as they should.

But this one in c++ isn't giving me the values it should.

My code is this one:

    #include <iostream>
    #include <stdlib.h>
    #include <string>
    #include <windows.h>
    #include <sstream>
    #include <mysql.h>
    #include <stdio.h>
    #include <vector>

    using namespace std;

    int q = 0;

    int numTotalUsers = 0;
    int numUsers2 = 0;
    int percentage = 0;
    int activeUsers = 0;

    int gatherTotalUsers();
    int gatherPercentage();
    int gatherActiveUsers();
    //int gatherTotalQuests();
    //int gatherActiveQuests();
    int showValues();

    int main()
    {   
        gatherTotalUsers();
        return 0;
    }

    int gatherTotalUsers()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"******","Pfw7lneUyi","********","Pfw7lneUyi",3306, NULL, 0);

        stringstream sU;
        sU << "SELECT * FROM usageTrack";

        string uT_query = sU.str();
        const char* _q = uT_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                numTotalUsers = (int)row[0];
            }
        }else
        {
            cout << "1";
        }

        gatherPercentage();
    }

    int gatherPercentage()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"******","Pfw7lneUyi","*******","Pfw7lneUyi",3306, NULL, 0);

        stringstream sU2;
        sU2 << "SELECT * FROM usageTrack WHERE usedTimes >= 2";

        string uT2_query = sU2.str();
        const char* _q = uT2_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                numUsers2 = (int)row[0];
            }
        }else
        {
            cout << "2";
        }

        percentage = ((int)numUsers2 * 100) / (int)numTotalUsers;

        gatherActiveUsers();
    }

    int gatherActiveUsers()
    {
        // mysql
        MYSQL_ROW row;
        MYSQL_RES* res;

        // users
        MYSQL* connU;
        connU = mysql_init(0);
        connU = mysql_real_connect(connU,"*****","Pfw7lneUyi","*********","Pfw7lneUyi",3306, NULL, 0);

        stringstream aU;
        aU << "SELECT * FROM activeUsers";

        string aU_query = aU.str();
        const char* _q = aU_query.c_str();

        q = mysql_query(connU, _q);

        if(!q)
        {
            res = mysql_store_result(connU);

            while(row = mysql_fetch_row(res))
            {
                activeUsers = (int)row[0];
            }
        }else
        {
            cout << "3";
        }

        showValues();
    }

    int showValues()
    {
        int r;

        cout << "Total Users: " << numTotalUsers << "\n% of returning users: " << percentage << "\nActive Users: " << activeUsers;

        cout << "\n\n[1] - refresh\n\n>>";

        cin >> r;

        if(r == 1)
        {
            showValues();
        }else
        {
            return 0;
        }
    }

The code compiles and debbugs without a single problem. The "error" is that the values I get at the last function are:

  • Total Users: 19147440
  • % of returning users: 100
  • Active Users: 19223608

And the values it should be giving are:

  • Total Users: 140
  • % of returning users: 22%
  • Active Users: 1

(These last values are taken from the dashboard made with php)

Aucun commentaire:

Enregistrer un commentaire