Hello

I'm getting a segmentation fault in the mysql function mysql_real_escape_string
and I don't have a clue why.  What am I missing?

#include <mysql.h> /* Headers for MySQL usage */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// #define INSERT_STATEMENT "INSERT INTO patient (idno,first,last,medrec) 
VALUES(NULL,?,?,?)"

static MYSQL clinical_db;
/* static MYSQL_STMT *stmt; */
/* static MYSQL_BIND cols[3]; */
static my_ulonglong affected_rows;
/*static int param_count; */
static unsigned long str_length;
char str_data[1024];
/* static my_bool is_null; */


int main(int argc, char **argv){
  int insert_id;
  char *encdata, *query, *end, *value;
  int datasize;
  int param_count;
  
  MYSQL_RES *res; /* To be used to fetch information into */
  MYSQL_ROW row;

  mysql_init(&clinical_db);
        
  if(mysql_real_connect(&clinical_db, "localhost", "pharmacy", "show22case", 
"clinical", 0, NULL, 0)){
          fprintf(stderr, "No Connection: %s\n", mysql_error(&clinical_db));
          exit(0);
  }
  printf ("Hey this worked\n");
  
/* Prepared statements not supported in this version of MYSQL */  
 
/*  stmt = mysql_stmt_init(clinical_db);  */
 
//  if(!stmt){
//       fprintf(stderr, "mysql_stmt_init(), out of memory\n");
//       exit(0);
// }
 
//  if(mysql_stmt_prepare(stmt, INSERT_STATEMENT, strlen(INSERT_STATEMENT))){
//      fprintf(stderr, "mysql_stmt_prepare(), INSERT failed\n");
//       fprintf(stderr, "%s\n", mysql_stmt_error(stmt));
//  }   

/* Get parameter count and hope it is right */
//param_count = mysql_stmt_param_count(stmt);
//fprintf(stdout, "total params are %d\n", param_count);

  /* Let's try to add something to the database */

query = malloc(2048);
value = malloc(2048);
encdata = malloc(2048);
strcpy(query,"INSERT INTO patient VALUES (NULL,");
str_length = strlen(query);
end = query + str_length + 1;
printf("\nFirst Name ==>");
fgets( value, 26, stdin);
mysql_real_escape_string(&clinical_db,encdata,value,strlen(value)); <<======= 
Segmenetation Fault
/*end += mysql_real_escape_string(&clinical_db,end,value,strlen(value));*/
*end++ = ',';
printf("\nLast Name ==>");
fgets( value, 26, stdin);
end += mysql_real_escape_string(&clinical_db, end,value,strlen(value) - 1);
*end++ = ',';
printf("\nMedical Record Number==>");
fgets(value,12,stdin);
end += mysql_real_escape_string(&clinical_db, end,value,strlen(value) - 1);
end++;
strcpy(end,")");

printf("\nResulting Query %s", query);



return 0;
} /* End of main() */


-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to