Sql Connection problem

Hey guys, i am trying to connect mySQL to a program in c++. I tried out a tutorial i found and here are my codings..
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

  #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;
    }

    /*The next step is to actually connect to the database. This is where the conn variable and the host,
    username, password, and database #define's are used:*/
    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;

    /*Finally, we query the database with the following query using the mysql_query() function: "SELECT * FROM users" like so:*/
    mysql_query(conn,"SELECT * FROM movies_info");

    /*After querying the result, we need to store the result data in the variable res_set we defined earlier. This is done like so:*/
    res_set = mysql_store_result(conn);

    /*In our example, there will most likely be more than one row returned (i.e., if there are more than one user in
    the users table of the database). If so, then you need to find how many rows are returned so that you can
    loop through each one and print the result of each one like so:*/
    unsigned int numrows = mysql_num_rows(res_set);
    unsigned int num_fields = mysql_num_fields(res_set);


    /*Finally, we retrive the result using the function mysql_fetch_row() and then print out all of the data in large chunks.
    This is done like so:*/
    while ((row = mysql_fetch_row(res_set)) != NULL)
    {




For some reason, when i compile, there are no errors, but when i try running it, error msgs will be displayed....


1
2
3
4
5
6
7
8
9
/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' 



What is the problem and how can i fix it?
Your errors it's not run time errors, it's compile time. So when you say "when i compile, there are no errors" you are wrong
Sorry for that mistake. do you know why such errors are appearing?
The first you need to check which header do you need to include in order to use "mysql_init"
and others functions
#include <mysql/mysql.h>


This is the header needed. I found a solution which says i have to add -Ldir_path -lmysqlclient at the end of your link. What do they mean by link and where is it? IM using netbeans.

Or is there another solution in solving this?
-L this option specifies path to library location
-l this option specifies a library name that should be used during linkage process.

Is there any errors or warnings that file mysql.h was not found?
Nope. there were no errors regarding that.

http://dev.mysql.com/doc/refman/5.1/en/link-errors.html


I went to read up on it, and they toldm e to add Ldir_path -lmysqlclient but where do i add this to? do i copy or paste it to somewhere or whay?
This options should be added via menu of NetBeans.
Please find project properties and find out where there are linkage options.
Problem solved. thanks alot!
Topic archived. No new replies allowed.