来源: http://blog.csdn.net/afeiluo/article/details/7756518

#include

#include

int main(int argc,char **argv){
   MYSQL mysql_conn;
   MYSQL_RES *mysql_result;
   MYSQL_ROW mysql_row;
   char execsql[256];
   int f1,f2,num_row,num_col;
   
   if(mysql_init(&mysql_conn)!=NULL)
    {

        if(mysql_real_connect(&mysql_conn,"localhost","root","111111","test",MYSQL_PORT,NULL,0)!=NULL)
          {
              printf("connect success!!!\n");
              sprintf(execsql,"select * from info");
              if(mysql_query(&mysql_conn,execsql)==0)
              {
                 printf("query success!\n");
                 mysql_result=mysql_store_result(&mysql_conn);
                 num_row=mysql_num_rows(mysql_result);
                 num_col=mysql_num_fields(mysql_result);
                 printf("row: %d,col: %d\n",num_row,num_col);
                    for(f1=0;f1
                       {
                          mysql_row=mysql_fetch_row(mysql_result);
                            for(f2=0;f2
                              {
                                printf("[Row %d,Col %d]==>[%s]\n",f1,f2,mysql_row[f2]);
                              }
                       }
             }else
              {
                 printf("query error!!!\n");
              } 
        }else
          {
             printf("connect failed\n");
          }
  }else
     {
        printf("initial failed\n");
     }
     mysql_free_result(mysql_result);
     mysql_close(&mysql_conn);
     return 0;
}
编译: gcc xxx.c -o yyy I/usr/include/mysql/  -L/usr/lib/mysql/ -lmysqlclient
01-02 22:21