The code for connecting to the database function is as follows: CString Person::ConnectMySQL(void) { CString failOrSuccess; //mysql_init(&m_sqlCon); // localhost: server root/1234 is the account password, cooldowntest is the database name, and 3306 is the port if(!mysql_real_connect(&m_sqlCon,"192.168.253.6","root","1234","cooldowntest",3306,NULL,0)) { //In mfc, the localhost of the statement connecting to the database should be changed to IP (my IP is 10.10.2.105); failOrSuccess=_T("Access to the database failed"); //CString e=mysql_error(&m_sqlCon); Originally there was such a code on the Internet, but there was a string conversion problem, so I modified it CString e; e.Format(_T("%d"), mysql_error(&m_sqlCon));//The character set in the project properties needs to be modified to °Use multi-byte character set" or not set AfxMessageBox( e ); //If the password is entered incorrectly, the error code 5098495 will pop up. //return; As a function, since there must be a return value, it is commented out } else { //AfxMessageBox(_T("Access to the database is successful")); failOrSuccess=_T("Access to the database succeeded"); mysql_query(&m_sqlCon,"SET NAMES 'GB2312'");//Solve the problem that Chinese characters are displayed in garbled characters after reading data in the database } return failOrSuccess; }
void InsertData(CString m_csPersonId, CString m_csPersonPwd); // Because connecting to the database, inserting, and deleting all require this variable or a pointer to this variable, it is defined as a member of the class MYSQL m_sqlCon; // Pass in the username and password, verify personal information, and return a string to determine whether the login succeeded or failed. There are two reasons for the failure, one is the wrong password, and the other is that the username does not exist CString IdentifyInfor(CString m_csPersonId, CString m_csPersonPwd); // statement of query result set MYSQL_RES *results; // query result row declaration MYSQL_ROW record;
Additional: Freshman C++ training, using mysql during the login and registration process