urgent: SEGMENTATION FAULT WHEN RUNNING C API ON LINUX SERVER

I am experiencing the error" segmentation fault (core dumped)"  on a linux server when compile using
gcc -I/usr/lib/mysql -L/usr/include/mysql mysql_client.c -o mysql_client -lm -lmysqlclient -lz

my code is as follows

#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include<string.h>
#include<unistd.h>

#define USE_OLD_FUNCTIONS 1
#define MAXLEN 200
#define NUMBER_OF_COLS 6
MYSQL mysql;
MYSQL_RES *res;
MYSQL_ROW row;

void exiterr(int exitcode)
{
    fprintf( stderr, "%s\n", mysql_error(&mysql) );
    exit( exitcode );
}
int mysql_exec_sql(MYSQL *mysql,const char *create_definition)
{
   return mysql_real_query(mysql,create_definition,strlen(create_definition));
}

int main()
{
/********************************************************************
VARIABLE DEFINITIONS AND INITIALIZAITONS
**********************************************************************/      
          uint i = 0;
      char buf[]="name,sex,disease";
      char record[MAXLEN],prev_record[NUMBER_OF_COLS][MAXLEN],diagnosis[MAXLEN];
      char *name,*sex,*disease;
      int ipat_id,ipat_status;
      char cpat_id[10], cpat_status[3];
      //sprintf(cpat_id, "%d", ipat_id);
      mysql_init(&mysql);
      strcpy(diagnosis,"");
/********************************************************************
CONNECTING TO TELEMEDICINE DATABASE ON MYSQL SERVER
**********************************************************************/      
    if (!(mysql_real_connect(&mysql,HOSTNAME,USERNAME,PASSWORD,DATABASENAME,0,NULL,0)) )
        {
            printf("there is some error in connection\n");
            exiterr(1);
      }

    if (mysql_select_db(&mysql,DATABASENAME))
        exiterr(2);
 /********************************************************************
FINDING LAST RECORD IN THE DATABASE
**********************************************************************/      
   if (mysql_query(&mysql,"SELECT * FROM patient ORDER BY pat_id ASC"))
        exiterr(3);

    if (!(res = mysql_store_result(&mysql)))
        exiterr(4);
   
    while((row = mysql_fetch_row(res))) {
        for (i=0 ; i < mysql_num_fields(res); i++)
       {
                 strcpy(prev_record[i],row[i]);
      }
    }      
/********************************************************************
THE INPUT TEXT FROM CLIENT IS IDNETIFIED IN VARIABLES
**********************************************************************/      
      name=strtok(buf,",");
      sex=strtok(NULL,",");
      disease=strtok(NULL,",");
      
/********************************************************************
SETTING UP CLIENT ID PROPERLY IN VARIABLES
**********************************************************************/      
      ipat_id=atoi(prev_record[0]);      //converting char to iteger
      ipat_id++;                  //setting up new id for the new record
      sprintf(cpat_id, "%d", ipat_id);//Again conv to char to be used in mysql query
      ipat_status = 1;
      sprintf(cpat_status, "%d", ipat_status);//Again conv to char to be used in mysql query
      
/********************************************************************
PRODUCE MYSQL QUERY IN REQ FORMAT IN 'RECORD' ARRAY
**********************************************************************/            
strcpy(record,"INSERT INTO `patient` ( `pat_id` , `pat_status`  , `pat_name` , `pat_sex` , `pat_disease` ) VALUES ('");
      strcat(record,cpat_id);
      strcat(record," ',' ");
      strcat(record,cpat_status);
      strcat(record," ',' ");
      strcat(record,name);
      strcat(record," ',' ");
      strcat(record,sex);
      strcat(record," ',' ");
      strcat(record,disease);
      strcat(record," ' ) ");
      
      printf("record = %s\n",record );

    if(mysql_exec_sql(&mysql,record)==0)/*success*/
    {
      printf( "Record Added\n");      
    }
else
    printf( "Failed to add records: Error: %s\n", mysql_error(&mysql));
/********************************************************************
WAITING FOR DOCTOR RESOPONSE
**********************************************************************/            

strcpy(record,"SELECT pat_diagnose FROM patient WHERE pat_id = '");
      strcat(record,cpat_id);
      strcat(record," ' ");

while(strlen(diagnosis) == 0)
{
      printf("Sleeping for 15 seconds\n");
      sleep(30);

      if (mysql_query(&mysql,record))
              exiterr(3);

      if (!(res = mysql_store_result(&mysql)))
              exiterr(4);
        //printf("NUMBER OF FIELDS = %d\n", mysql_num_fields(res));
       row = mysql_fetch_row(res);
          strcpy(diagnosis,row[0]);
          //printf("DIAGNOSIS = %s length = %d\n" , diagnosis, strlen(diagnosis));
 }               
/********************************************************************
DISPLAYING ALL THE RECORDS PRESENT IN THE TABLE
**********************************************************************/            
      if (mysql_query(&mysql,"SELECT * FROM patient"))
        exiterr(3);

    if (!(res = mysql_store_result(&mysql)))
        exiterr(4);
    while((row = mysql_fetch_row(res))) {
        for (i=0 ; i < mysql_num_fields(res); i++)
       {
                 printf("%s\n", row[i]);
      }
    }
    if (!mysql_eof(res))
        exiterr(5);

strcpy(record,"UPDATE patient SET pat_status = 0 WHERE pat_id = '");
      strcat(record,cpat_id);
      strcat(record," ' ");
      
//      printf("record = %s\n",record );

    if(mysql_exec_sql(&mysql,record)==0)/*success*/
    {
      printf( "Record Added\n");      
    }
else
    printf( "Failed to add records: Error: %s\n", mysql_error(&mysql));



/********************************************************************
CLOSING THE CONNECTION TO DATABASE
**********************************************************************/            
mysql_free_result(res);
mysql_close(&mysql);
    return 0;

}
      
ibm979Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joe JenkinsCommented:
Hmm.  Have you backtraced your core file to see where the segfault occured?  That will be a good start to fixing this problem.  You should have gdb.


1. gdb <executable> core

2. Use bt to see what happened.

3. Type select-frame <number>, where <number> is the number that appeared to the left of the name in the last level called.

4. Type list <source file>:<line> to get a listing of that line of code and a number of lines around it.

5. Type print <variable name> to see the value of a variable.  Does this look like what it should be?  Are you passing a null pointer or something weird?

6. Type q when done.

That's a basic outline of how you can get the values for what's going on here.  

Joe Jenkins
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Joe JenkinsCommented:
Any follow-up on this?

Joe Jenkins
0
ibm979Author Commented:
hi

What i found that the server where i was trying the compile the program has very old version of MYSQL. I assumed that problem was with that old software. Bcz i compiled the same code on my local machine with latest version of MYSQL and it come with out any error and working properly.

regards
0
Joe JenkinsCommented:
ibm979

Ah, I'm glad you found the problem.  I looked through your code and didn't see anything that stuck out to me.  I do most of my programming in mysql 5 or MSSQL so it all made sense to me.

Grats!
Joe
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.