0

Possible Duplicate:
How to make #include <mysql.h> work?

I need to connect C and mysql

This is my program

#include <stdio.h>
#include <mysql.h>
#define host "localhost"
#define username "root"
#define password "viswa"
#define database "dbase"
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 name, email, password FROM users");


    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;

}

I got the error "unable to include mysql.h".

I am using windows 7, Turbo C, mysql and I downloaded mysql-connector-c-noinstall-6.0.2-win32-vs2005, but I don't know how to include it.

Community
  • 1
  • 1
Viswa
  • 1
  • 1
  • 3
  • 5
    "Windows 7 and Turbo C" should be the title of a movie, preferably some over-the-top bollywood piece with dancing pointers. – Kerrek SB Feb 25 '12 at 16:15
  • 2
    Have you taken a look at [this](http://stackoverflow.com/questions/2516187/how-to-make-include-mysql-h-work) and [this](http://stackoverflow.com/questions/4290249/mysql-mysql-h-file-not-found-in-vs2008-c-beginner-question) ? – Coren Feb 25 '12 at 17:40

1 Answers1

3

Wrong syntax. The #include is a C preprocessor directive, not a statement (so should not end with a semi-colon). You should use

#include <mysql.h>

and you may need instead to have

#include <mysql/mysql.h>

or to pass -I /some/dir options to your compiler (with /some/dir replaced by the directory containing the mysql.h header).

Likewise, your #define should very probably not be ended with a semi-colon, you may need

#define username "root"
#define password "viswa"
#define database "dbase"

I strongly suggest reading a good book on C programming. You may want to examine the preprocessed form of your source code; when using gcc you could invoke it as gcc -C -E yoursource.c to get the preprocessed form.

I also strongly recommend enabling warnings and debugging info (e.g. gcc -Wall -g for GCC). Find out how your specific compiler should be used. Learn also how to use your debugger (e.g. gdb). Study also existing C programs (notably free software).

You should learn how to configure your compiler to use extra include directories, and to link extra libraries.

N.B. With a linux distribution, you'll just have to install the appropriate packages and perhaps use mysql_config inside our Makefile (of course you'll need appropriate compiler and linker flags), perhaps with lines like

CFLAGS += -g -Wall $(shell  mysql_config --cflags)
LIBES += $(shell mysql_config --libs)

added to your Makefile.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • ya your right about coding, it just copy past error, my question is how to clear "unable to open include file mysql.h" error – Viswa Feb 25 '12 at 16:23
  • You need to pass the correct directories thru `-I /some/dir/` options to your compiler.... I mentioned that briefly in my answer... – Basile Starynkevitch Feb 25 '12 at 17:46
  • i am using turbo c in windows, please tell me clearly... i can't understood – Viswa Feb 25 '12 at 18:15
  • Then you need to tell your compiler where are the include directories (and the library files) for the library you are linking to. I have no idea what *Turbo C* means these days (I have only used `Turbo C` version 2 on MSDOS more than twenty years ago...). – Basile Starynkevitch Feb 25 '12 at 18:17
  • turbo c is old compiler, please i need to create c program for displaying one table from mysql database, so which compiler i need, i am using windows 7 and mysql datadase – Viswa Feb 25 '12 at 18:22
  • I believe that any C99 standard conforming compiler should be ok. I think that you should *learn* (by reading suitable documentation) how to configure and to use it! – Basile Starynkevitch Feb 25 '12 at 18:27
  • please tell me compiler name clearly so that i can download it – Viswa Feb 25 '12 at 18:31
  • I'm suggesting instead to install a good Linux distribution, e.g. http://debian.org/ on your computer... And I never used Windows (I'm using Linux since 1993) so I am the wrong person to ask about it... – Basile Starynkevitch Feb 25 '12 at 18:33
  • 1
    ok, i also installed ubuntu 10.10 in my system – Viswa Feb 25 '12 at 18:38
  • ok friend, i will try to find solution some where, thank you for your effort – Viswa Feb 25 '12 at 18:43