MySQL SELECT statement isn't executing

I am trying to fetch data from the MySQL database using the MySQL Connector library. But the program isn't returning any data from the database. Please any suggestions? I have data in the MySQL database. I attached a picture of the Serial monitor output.

int device_id = 1;
String Parameter[3] = {"one", "two", "three"};

EthernetClient client;
MySQL_Connection conn((Client *)&client);

const char QUERY_PARA[] = "SELECT ref_time, val_first, val_second  FROM test_db.test_table WHERE Id=  %lu;";
char query[128];

void setup()
{
    Serial.begin(115200);
    while (!Serial);
    Ethernet.begin(mac_addr);   
}

void loop()
{
    Serial.println("inside the loop....");
    delay(10000);
    check_parameter();
}


void check_parameter()
{
  if (conn.connect(server_addr, 3306, user, password))
    {
        Serial.println("calling the check_para....");
        delay(5000); 
        
        MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
        sprintf(query, QUERY_PARA, device_id);

        cur_mem->execute(query);
        column_names *cols = cur_mem->get_columns();

        Serial.println();
        row_values *row = NULL;

        do
        {
            row = cur_mem->get_next_row();
            if (row != NULL)
            {
              // here is the problem. row is always NULL! I dunno why ??????

                Parameter[0] = String(row->values[0]);
                Parameter[1] = String(row->values[1]);
                Parameter[2] = String(row->values[2]);
                
                cur_mem->free_row_buffer();
            }
        } while (row != NULL);
        cur_mem->free_columns_buffer();
        delete cur_mem;
        
        Serial.println("printing parameters");
        Serial.println(Parameter[0]);
        Serial.println(Parameter[1]);
        Serial.println(Parameter[2]);

        conn.close();
    }
    else
    {
        Serial.println("Connection failed.");
    }   
}

Connecting Arduino directly to MySQL is not a good way. It's insecure and inflexible. It is better to use HTTPS with PHP server. See detail in Arduino - MySQL tutorial

Unfortunately, I couldn't use an extra server in my case.

...
int device_id ...
...
const char QUERY_PARA[] = "... WHERE Id=  %lu;";
...

:o

Possibly that is only a difference between the name device_id used in the C++ code and the SQL column name Id. Or do you mean %d would be better than %lu ?

I'd print out the value of query just after this statement:

sprintf(query, QUERY_PARA, device_id);

then issue exactly that resolved query directly in the mySql console to see if it brings anything back.

6v6gt:
Or do you mean %d would be better than %lu ?

What happens when a 16 bit int is formatted as an unsigned long.

Caller provides 16 bits. Callee consumes 32 bits. The result is?

Look at the dB log. Most of the mysql servers require that the remote connections need to be allowed/enabled.

The dB log should give the who, what, where and the why of the remote connection. The IP address of the refused connection can be added to the remote connection allow list.

If you used http get/post, and so forth and so on the code can be passed as parameters to a web page that runs locally to the server.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.