Get ip and Check in the database (MySQL)

Hello everybody,

I'm trying to make a system to check that the user ip exists in the database, but is not working

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include <windows.h> 
#include <wininet.h> 
#include <sstream>
#include <string>
#include <iostream>
#include <stdio.h> 
#include <winsock.h> 
#include <conio.h>
WSADATA data; 
SOCKET winsock; 
SOCKADDR_IN sock; 
#pragma comment(lib, "wininet")
#pragma comment(lib, "wsock32.lib")
#include <C:\Dev-Cpp\include\mysql.h> 
#define OpenWebsite(a) ShellExecute(NULL,"open",a,NULL,NULL,SW_SHOWNORMAL); 

using namespace std;


int main() {

    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char *server = "localhost";
    char *user = "root";
    char *password = "";
    char *database = "AEX";
    conn = mysql_init(0);

    if (!mysql_real_connect(conn, server,
       user, password, database, 0, NULL, 0)) {
       fprintf(stderr, "%s\n", mysql_error(conn));
       exit(1);
    }
    ostringstream strstr;
    
    HINTERNET hInternet, hFile;
    DWORD rSize;
    char getip[47];

    hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
    hFile = InternetOpenUrl(hInternet, "http://icanhazip.com", NULL, 0, INTERNET_FLAG_RELOAD, 0);
    InternetReadFile(hFile, &getip, sizeof(getip), &rSize);
    getip[rSize] = '\0';

    InternetCloseHandle(hFile);
    InternetCloseHandle(hInternet);

    strstr << "SELECT * FROM users WHERE IP='" + getip +"'" ;
    
    string str = strstr.str();

    if(mysql_query(conn, str.c_str())) {
        fprintf(stderr, "%s\n", mysql_error(conn));
    }
    res = mysql_use_result(conn);
    while (row = mysql_fetch_row(res)) {
      OpenWebsite(row[2]);
      std::cout << "WEBSITE: " << row[2] << "";
    }
    mysql_free_result(res);
    mysql_close(conn);   
    system("PAUSE");
    return false;
}


This error:
invalid operands of types `const char[35]' and `char[47]' to binary `operator+'

Line:
strstr << "SELECT * FROM users WHERE IP='" + getip +"'" ;
Last edited on
What happens that tells you it isn't working? Have you also tried running the query manually and confirming it works?
Yes, I tried more does not work.

Before I was with the same design in visual studio and there I used
gcnew String (getip), but now I'm in dev c ++ and I can not verify the IP
Yes, I tried more does not work.

Do you mean you ran the query manually and it failed? "tried more" does not tell me anything.
Before I was with the same design in visual studio and there I used
gcnew String (getip), but now I'm in dev c ++ and I can not verify the IP

Or do you mean you tried this?

You still haven't told be why you don't think it is working. Is it reporting an error?
When I use "+" gives this error

invalid operands of types `const char[35]' and `char[47]' to binary `operator+'


When I use << not of any error, but it seems that the variable is empty, I use printf to display in cmd appears my exact ip, and in the database there is my ip registered, but does not return anything.

I said that in Visual Studio I worked because I used other functions that require .NET 4 and other dlls.
That particular error comes from trying to use + to concatenate to C-style strings, which is not legal. Using << like you were was correct. You should check what the query string is before sending it, and also check what (if any) errors the query is giving.
Topic archived. No new replies allowed.