c++ mysql error

Hi i did a program with a mysql auth system and it returns errors but i dont find them,would be thankful if someone help me!
login.cpp code:

#include "login.h"
#include <iostream>


struct auth {
char* address;
char* username;
char* password;
char* database;
int port;
};



int login_check(char* username, char* password, std::string hwid, MYSQL* & sql) {
auth login;
login.address = "address";
login.username = "user";
login.password = "password";
login.database = "database";
login.port = 3306;
sql = mysql_init(sql);
if (mysql_real_connect(sql, login.address, login.username, login.password, login.database, login.port, nullptr, NULL)) {
printf("Failed to Connect: %s\n", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_CONNECTION;
}
else
{
printf("Server is OK!\n");
}
std::string query = std::string( "SELECT * FROM users WHERE ") + " hwid='" + hwid + "' AND toggled=true AND username='" + username +"' AND password = '" + password + "';" ;
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}

MYSQL_RES* result = mysql_store_result(sql);



if (result)
{
printf("Login is OK");
mysql_close(sql);
sql = NULL;
return SUCCESS;
}
else
{

printf("Bad Login.");
mysql_close(sql);
sql = NULL;
return BADLOGIN;
}
}

int register_user(char* username, char* password, std::string hwid, MYSQL*& sql) {
auth login;
login.address = "address";
login.username = "username";
login.password = "password";
login.database = "database";
login.port = 3306;
sql = mysql_init(sql);
if (mysql_real_connect(sql, login.address, login.username, login.password, login.database, login.port, nullptr, NULL)) {
printf("Failed to Connect: %s\n", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_CONNECTION;
}
else
{
printf("Server is OK!\n");
}
std::string query = std::string("SELECT * FROM users WHERE username = '") + username + "';";
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}

MYSQL_RES* result = mysql_store_result(sql);



if (result)
{
printf("User already exists");
mysql_close(sql);
sql = NULL;
return SUCCESS;
}
else
{
query = std::string("INSERT IGNORE INTO `autoclicker`.`users` (`hwid`, `toggled`, `username`, `password`) VALUES ('" + hwid + "', 'false', '" + username + "', '" + password + "');");
std::cout << query;
if (!mysql_query(sql, query.c_str())) {
printf("Failed to register: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}
else {
std::string query = std::string("SELECT * FROM users WHERE username = '") + username + "';";
if (!mysql_query(sql, query.c_str())) {
printf("Failed to query for login: %s", mysql_error(sql));
mysql_close(sql);
sql = NULL;
return FAILED_QUERY;
}

MYSQL_RES* result2 = mysql_store_result(sql);
if (result2) {
mysql_close(sql);
sql = NULL;
printf("\nRegistered Sucessfully!");
return BADLOGIN;
}
else
{
mysql_close(sql);
sql = NULL;
const char *teste = mysql_error(sql);

return 1;
}

}

}
}


login.h code:

#pragma once
#include <mysql.h>
#include <string>
#include "picosha2.h"
#pragma comment(lib,"libmysql.lib")

#define FAILED_CONNECTION -1
#define FAILED_QUERY -2
#define BADLOGIN 0
#define SUCCESS 1


int login_check(char* username, char* password, std::string hwid, MYSQL*& sql);
int register_user(char* username, char* password, std::string hwid, MYSQL*& sql);
Last edited on
what exact errors?
Failed to Connect: Bad handshake

image: http://prntscr.com/n5ki96
Last edited on
Anyone could help?
Is "address" actually a valid address? (Along with the other things passed into mysql_real_connect) Or did you just write that when posting this?

According to https://dev.mysql.com/doc/refman/8.0/en/mysql-real-connect.html
If host is NULL or the string "localhost", a connection to the local host is assumed

Perhaps that's what you want, if you're connecting to a sql database locally. There's also other potential tips on that page linked above that might be useful.

Edit:
You should also check the return value of sql = mysql_init(sql);
That's the first thing you should do, before you call mysql_real_connect.
https://dev.mysql.com/doc/refman/5.7/en/mysql-init.html

Last edited on
you are out of my area too far. I would offer that you should connect to the DB directly with a sql engine / tool (or excel or the like) to ensure you can actually connect to it with something you didn't write. If commercial tools can't connect, you can't either.... some DB need you to install/register/stuff before you can talk to them.
Topic archived. No new replies allowed.