Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to connect c and mysql

this is my program

C++
#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 error "unabel 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 dont know how to include it

thank you
Posted
Updated 25-Feb-12 21:40pm
Comments
psgviscom 25-Feb-12 7:15am    
did you add reference of mysql to your application.
viswa317 25-Feb-12 8:24am    
i don't understood
psgviscom 25-Feb-12 13:40pm    
If you are using vs 2005. Then you should add reference, so that only it will find mysql.h.

It doesn't know where to find mysql.h.
 
Share this answer
 
Comments
viswa317 25-Feb-12 8:23am    
how to solve this problem
#realJSOP 25-Feb-12 10:42am    
Well, you add it to your project.
You should add the folder containing the mysql.h file to the include search path of your compiler.
 
Share this answer
 
Comments
viswa317 26-Feb-12 0:04am    
yes, that what i dont know how to do that....... please explan
The syntax of your include statements is incorrect, it's missing the delimiters, it should be:
C++
#include <mysql.h>
// or
#include "mysql.h"

depending on whether mysql.h is in the system include directories or one of the project directories.
 
Share this answer
 
Follow the steps to add reference
project-> add reference-> "your reference"(mysql.h)
(This will workout if you use visual studio.)
 
Share this answer
 
Comments
Richard MacCutchan 26-Feb-12 3:52am    
In C/C++?
I used Linux, in windows i find it difficult.. now i got output perfectly
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900