CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    24

    Connection database problem

    Hey guys im trying to connect my database to a c++ program. I tried out a tutorial regarding this issue and here are my codings...


    Code:
          #include <stdio.h>
          #include <stdlib.h>
          #include <mysql/mysql.h>
          #include <string>
           #define host "localhost"
           #define username "root"
    #define password "password"
    #define database "movies"
    
    
    MYSQL *conn;
          int main()
    
          {
    
    MYSQL_RES *res_set;
        MYSQL_ROW row;
    
       conn = mysql_init(NULL);
    
        if( conn == NULL )
        {
            printf("Failed to initate MySQL\n");
            return 1;
        }
    
      
        if( ! mysql_real_connect(conn,host,username,password,database,0,NULL,0) )
        {
            printf( "Error connecting to database: %s\n", mysql_error(conn));
            return 1;
        }
    
        unsigned int i;
    
        mysql_query(conn,"SELECT * FROM movies_info");
    
       
        res_set = mysql_store_result(conn);
    
        unsigned int numrows = mysql_num_rows(res_set);
        unsigned int num_fields = mysql_num_fields(res_set);
    
       
        while ((row = mysql_fetch_row(res_set)) != NULL)
        {
    
            for(i = 0; i < num_fields; i++)
            {
                 printf("%s\t", row[i] ? row[i] : "NULL");
            }
            printf("\n");
    
        }
    
        
        mysql_close(conn);
        return 0;
    
          }
    When i compile it, everything seems fine, but when i run it, error msgs like below appear.

    Code:
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:37: undefined reference to `mysql_init'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:49: undefined reference to `mysql_real_connect'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:51: undefined reference to `mysql_error'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:58: undefined reference to `mysql_query'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:61: undefined reference to `mysql_store_result'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:66: undefined reference to `mysql_num_rows'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:67: undefined reference to `mysql_num_fields'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:72: undefined reference to `mysql_fetch_row'
    /home/user/NetBeansProjects/Databasetest/newmain.cpp:84: undefined reference to `mysql_close'
    all the errors have to do with the code MYSQL *conn;... the lines which the errors are in all have *conn in them. is it because the program is not reading the mysql libraries?
    Last edited by gregarion; February 9th, 2010 at 04:31 AM.

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Connection database problem

    Please use code tags for your code !!! Also make sure it is properly indented!
    Quote Originally Posted by gregarion View Post
    When i compile it, everything seems fine, but when i run it, error msgs like below appear.


    /home/user/NetBeansProjects/Databasetest/newmain.cpp:37: undefined reference to `mysql_init'
    These are compiler errors, so what do you mean with your statement above? Please point out line 37 in your code!
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Jan 2010
    Posts
    24

    Re: Connection database problem

    Sorry treuss, i edited it already. it seems that it is unable to read the libraries? it all starts off with this MYSQL *conn; ? i myself am not really sure what is causing the problem.

  4. #4
    Join Date
    Jan 2010
    Posts
    24

    Re: Connection database problem

    Problem solved

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured