Hi all. I'd like to ask for a way to store a MySQL table content of type VARCHAR to a CHAR variable in C. The MySQL table has three columns: ID (int), Name (varchar(22)), Salary (int). My C program should ask the user to input an ID number, and depending on the input value, the program should look up in the table whose Name it belongs to, then store that name on a variable of type CHAR. I tried doing it but an error that says "Incompatible variable types" is returned when I compile the program. The function of my program that handles this functionality looks like the one below:

struct data_t {
	char epc[22];

struct data_t data;

void command1(MYSQL *conn, int input) { //retrieves Name to which the input ID belongs
  int num_rows;
  MYSQL_RES *res;
  MYSQL_ROW row;

  char cmd2[1024] = "";
  snprintf(cmd2,sizeof(cmd2),"select Name from table8 where id = %d", input);
  if (mysql_query(conn, cmd2)) {
      fprintf(stderr, "%s\n", mysql_error(conn));

  res = mysql_use_result(conn);     
  row = mysql_fetch_row(res);
  data.epc = row[0];  


At the line "data.epc = row[0];" above is where the error occurs. Are there other steps that I have to do first so that I could store the VARCHAR result into a CHAR variable in C? Answers/suggestions will be very much appreciated. Thanks in advance!

PS: The structure contains other variables other than char epc[22] but I didn't include them here since they're not relevant to the topic. And by the way, WindowsXP is my operating system, but I hope Linux users could also give me answers. The syntax are very much similar anyway.

6 Years
Discussion Span
Last Post by davidchilders

Have you posted this question in the C programming forum?

This really has very little to do with MySQL (your MySQL syntax "select Name from table8 where id = %d" seems correct). Your problem lies with how C is handling the character type that is returned.

I hope this helps!

/David C.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.