0

I use the C api mysql, however, I encounter an error when I am connected to the database, and I chose the selected item. "Commands out of sync; you can't run this command now"

boolean
check_token(char *token) {
    MYSQL_RES   *res;
    MYSQL_ROW   row;
    char        *pass;
    char        query[200];

    snprintf(query, 200, "SELECT * FROM tokens WHERE token = \"%s\"", token); //  SELECT * FROM tokens WHERE token = "84ds4d3d3sd453s"
    if (mysql_query(&db, query)) {
        fprintf(stderr, "%s\n", mysql_error(&db));
        return (FALSE);
    }
    printf("OK\n");
    if (!(res = mysql_store_result(&db))) {
        fprintf(stderr, "%s\n", mysql_error(&db));
        return (FALSE);
    }
    if (!(row = mysql_fetch_row(res)))
        return (FALSE);
    pass = row[1];
    printf("Le token est %s\n", pass);
    mysql_free_result(res);
    return (TRUE);
}

No further requests are made just before. Just the connection to the database that is successful.

7hsk
  • 335
  • 2
  • 10
  • Where exactly doe this occur? On `if (mysql_query(&db, query)) {`? It sounds like you've still got another query executing that you haven't freed. – Martin Jun 02 '19 at 08:47

1 Answers1

0

There is a chance that you will not free the MYSQL_RES if mysql_fetch_row call fails, making next subsequent calls fail.

free the result before returning from the function.

if (!(row = mysql_fetch_row(res)))
{
    mysql_free_result(res); //Free before returning
    return (FALSE);
}

pass = row[1];
printf("Le token est %s\n", pass);
mysql_free_result(res);
return (TRUE);
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44