Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I just want to connect mysql server using c on windows xp and add some fields to the data base. I tried the following code but geting an error like unable to include mysql.h. Can any one help me? Thanks in advance.

#include<mysql.h>
#include<stdio.h>
int main() {
   MYSQL *conn;
   MYSQL_RES *res;
   MYSQL_ROW row;
   char *server = "localhost";
   char *user = "root";
   char *password = "user";
   char *database = "";
   
   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));
      return(0);
   }
   /* send SQL query */
   if (mysql_query(conn, "SELECT * FROM people WHERE age > 30")) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      return(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);
}
Posted
Updated 20-Apr-10 1:01am
v4

You need to add your includes and libs to the compilation. You can find and add mysqlclient.lib to your libraries, seems it is not added to your IDE/compiler.
 
Share this answer
 
I will highly recommend using MySQL++[^]. It is much easier then using MySQL C API directly.

-Saurabh
 
Share this answer
 
Just go to project properties -> Configuration properties -> Linker.
Goto to ->General and set the 'Additional Library directories' to point to your lib file directory.

Then goto to Linker -> Input and type in your lib file name e.g. mysqlclient.lib in the 'Additional Dependencies' field.

This is for VC++, if you are using some other compiler then you need to figure it out. I hope this should work for you.

You can even try this link Using libraries with Visual Studio 2005 Express[^]
 
Share this answer
 
v2
 
Share this answer
 
Comments
CHill60 9-Feb-14 8:52am    
Reason for my downvote = More than 3 years late with this and the OP specifically mentioned XP as the operating system

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