I'm making a RPG item/magic manager, and I'm trying to handle the case if the person choose a number that does not exist, but mysql_fetch_row keeps returning NULL when it runs for the second time.
I think I would need to run res = mysql_store_result(conn);
again, but it gives me this error if I try to run it inside the second while:
An exception was thrown at 0x00007FF82E124216 (libmysql.dll) (in rpg.exe): 0xC0000005: An access violation occurred while reading location 0x0000000000000010
This is my current code:
void listarPersonagem(MYSQL* conn) {
MYSQL_ROW row;
MYSQL_RES* res;
std::string query = "SELECT * FROM personagem";
const char* q = query.c_str();
qstate = mysql_query(conn, q);
int escolha = -1;
std::map<int, int> indexMap;
if(!qstate) {
res = mysql_store_result(conn);
signed int fields = static_cast<int>(mysql_num_rows(res));
while(escolha < 0 || escolha > fields) {
system("cls");
std::cout << "\t\t\t\t Personagens\n" << std::endl;
indexMap = {};
int i = 1;
while(row = mysql_fetch_row(res)) { //It returns NULL and doesn't run
std::cout << i << " - " << row[1] << std::endl;
indexMap.insert({i, std::atoi(row[0])});
i++;
}
std::cout << "0 - Voltar" << std::endl;
std::cout << "\n\n=>";
std::cin >> escolha;
}
}
int id = escolha != 0 ? indexMap[escolha] : 0;
row = NULL;
res = nullptr;
qstate = NULL;
}
Informations that may be useful:
IDE: Visual Studio 2019
OS: Windows 10