Thread: Connecting to a mysql server and querying problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    17

    Connecting to a mysql server and querying problem

    Im using a library to do this. This one: libmySQL.lib

    This is my code:
    Code:
    #include <mysql.h>
    #include <stdio.h>
    
    main() {
       MYSQL *conn;
       MYSQL_RES *res;
       MYSQL_ROW row;
    
       char *server = "localhost";
       char *user = "root";
       char *password = "";
       char *database = "KKI";
       
       conn = mysql_init(NULL);
       
       /* Connect to database */
       if (!mysql_real_connect(conn, server,
             user, password, database, 0, NULL, 0)) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(0);
       }
    
       /* send SQL query */
       if (mysql_query(conn, "SELECT * FROM KKI_users WHERE ID=1")) {
          fprintf(stderr, "%s\n", mysql_error(conn));
          exit(0);
       }
    
       res = mysql_use_result(conn);
       
       /* output fields 1 and 2 of each row */
       while ((row = mysql_fetch_row(res)) != NULL)
          printf("%s %s\n", row[1], row[2]);
    
       /* Release memory used to store results and close connection */
       mysql_free_result(res);
       mysql_close(conn);
    }
    It should work since it came from a tutorial.

    This is my compiler-log:

    Code:
    Compiler: Default compiler
    Building Makefile: "C:\test3\Makefile.win"
    Bezig met uitvoeren van  make...
    make.exe -f "C:\test3\Makefile.win" all
    gcc.exe -c main.c -o main.o -I"C:/Dev-Cpp/include"   
    
    In file included from C:/Dev-Cpp/include/mysql.h:68,
                     from main.c:1:
    C:/Dev-Cpp/include/mysql_com.h:115: error: syntax error before "SOCKET"
    
    C:/Dev-Cpp/include/mysql_com.h:115: warning: no semicolon at end of struct or union
    
    C:/Dev-Cpp/include/mysql_com.h:130: error: syntax error before '}' token
    C:/Dev-Cpp/include/mysql_com.h:130: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql_com.h:160: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:161: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:162: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:163: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:164: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:165: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:167: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql_com.h:168: error: syntax error before '*' token
    
    In file included from main.c:1:
    C:/Dev-Cpp/include/mysql.h:141: error: syntax error before "NET"
    C:/Dev-Cpp/include/mysql.h:141: warning: no semicolon at end of struct or union
    C:/Dev-Cpp/include/mysql.h:163: error: syntax error before '}' token
    C:/Dev-Cpp/include/mysql.h:163: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:176: error: syntax error before "MYSQL"
    C:/Dev-Cpp/include/mysql.h:176: warning: no semicolon at end of struct or union
    C:/Dev-Cpp/include/mysql.h:178: error: syntax error before '}' token
    C:/Dev-Cpp/include/mysql.h:178: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:183: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:184: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:185: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:186: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:188: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:189: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:190: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:192: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:193: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:194: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:195: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:196: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:197: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:198: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:199: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:201: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:201: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:201: warning: data definition has no type or storage class
    
    C:/Dev-Cpp/include/mysql.h:209: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:209: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:210: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:211: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:214: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:214: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:220: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:229: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:230: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:231: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:232: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:233: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:234: error: syntax error before '*' token
    
    C:/Dev-Cpp/include/mysql.h:236: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:238: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:239: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:240: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:241: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:242: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:244: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:245: error: syntax error before '*' token
    
    C:/Dev-Cpp/include/mysql.h:246: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:247: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:249: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:250: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:251: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:251: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:251: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:252: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:252: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:252: warning: data definition has no type or storage class
    
    C:/Dev-Cpp/include/mysql.h:253: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:253: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:254: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:255: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:255: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:255: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:256: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:256: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:256: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:257: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:257: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:257: warning: data definition has no type or storage class
    C:/Dev-Cpp/include/mysql.h:258: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:260: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:261: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:263: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:264: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:266: error: syntax error before '*' token
    
    C:/Dev-Cpp/include/mysql.h:267: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:268: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:271: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:275: error: syntax error before '*' token
    C:/Dev-Cpp/include/mysql.h:284: error: syntax error before ')' token
    C:/Dev-Cpp/include/mysql.h:285: error: syntax error before '*' token
    main.c: In function `main':
    main.c:5: error: `conn' undeclared (first use in this function)
    main.c:5: error: (Each undeclared identifier is reported only once
    main.c:5: error: for each function it appears in.)
    main.c:6: error: `res' undeclared (first use in this function)
    
    make.exe: *** [main.o] Error 1
    
    Uitvoering voltooid
    I'm using Dev-C++ 4.9.9.2. I have uploaded both mysql.h and mysql_com.h

    I'm a newbie in the C++ language and im just using it to make a dll so i can get an easier access to a mysql server in VB6.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Put a #include <winsock.h> before #include <mysql.h>

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    That seemed to help a bit, but now i get this:

    Code:
    Compiler: Default compiler
    Building Makefile: "C:\test3\Makefile.win"
    Bezig met uitvoeren van  make...
    make.exe -f "C:\test3\Makefile.win" all
    gcc.exe main.o  -o "Project 2.exe" -L"C:/Dev-Cpp/lib" ../Dev-Cpp/lib/mysql++.lib  
    
    main.o(.text+0x4e):main.c: undefined reference to `mysql_init@4'
    main.o(.text+0x93):main.c: undefined reference to `mysql_real_connect@32'
    main.o(.text+0xa5):main.c: undefined reference to `mysql_error@4'
    main.o(.text+0xe3):main.c: undefined reference to `mysql_query@8'
    main.o(.text+0xf5):main.c: undefined reference to `mysql_error@4'
    
    main.o(.text+0x12b):main.c: undefined reference to `mysql_use_result@4'
    main.o(.text+0x13c):main.c: undefined reference to `mysql_fetch_row@4'
    main.o(.text+0x179):main.c: undefined reference to `mysql_free_result@4'
    main.o(.text+0x187):main.c: undefined reference to `mysql_close@4'
    collect2: ld returned 1 exit status
    
    make.exe: *** ["Project] Error 1
    
    Uitvoering voltooid

  4. #4
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Seems like you forgot to link libmysql.lib. I haven't used mysql++.lib but I don't think it can stand on it's own feet.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Nope, even if i link libmysql it doenst work, not even together

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    I have no problem compiling it using "bcc32 mysql.c libmysql.lib". I don have Dev-Cpp installed so I can't test it with Mingw

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Is the bcc compiler for windows and if so where can i get it? (ive looked a little bit around on google but couldnt find it)

  8. #8
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    bcc32 is the Borland command-line compiler, but I don't think the compiler should matter much.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Hmm then why doesnt it work?

    EDIT: Problem solved. I opened up Microsoft Visual C++ and linked to the library, then i build it, with 1 warning (something about main, but nm) and i first got an error about libmySQL.dll, and then i went into the lib folder, got me the libmySQL.dll and copied it into the folder where my project was compiled in. Then it worked flawless

    Thanks for your help mate
    Last edited by Diod; 02-13-2006 at 12:15 PM. Reason: problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MySQL libraries
    By csonx_p in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2008, 02:23 AM
  2. Query multiple servers quickly
    By Stack Overflow in forum Networking/Device Communication
    Replies: 8
    Last Post: 05-09-2008, 11:04 AM