Hi everyone! I recently created a MySQL table that has three columns:
(1) integer ID (2) integer num1 (3) integer num2

Now, I have a C program that waits for the user to input an integer ID and when there is an input, the corresponding num1 and num2 in the 2nd and 3rd columns in the table will be retrieved. However, it only assumes that the input is in the table. I don't know yet how to include an error checking part in the code that will determine whether the inputted value is contained in the table or not. If the input is not in the table, I should inform the user that the input was invalid. Could anyone give me an idea how to implement this error checking? I'm using C program in Windows XP. Answers will be greatly appreciated.

My current program looks like the one below:

void command2(MYSQL *conn, int value) {
  int col1, col2;
  MYSQL_RES *res;
  MYSQL_ROW row;
  
  char cmd2[1024] = "";
  snprintf(cmd2,sizeof(cmd2),"select num1, num2 from table3 where ID = %d", value);
  if (mysql_query(conn, cmd2)) {
      fprintf(stderr, "%s\n", mysql_error(conn));
      exit(1);
  }
       
  res = mysql_use_result(conn);     
  while((row = mysql_fetch_row(res)) != NULL) {
      col1 = atoi((const char *) row[0]);
      col2 = atoi((const char *) row[1]);
  }
  mysql_free_result(res);
}

I don't know the C interface but assume it's the same as PHP's. If so, you could use the mysql_num_rows(res) result, or you could first check the row count by select count(*) from table3 where ID = %d

I don't know the C interface but assume it's the same as PHP's. If so, you could use the mysql_num_rows(res) result, or you could first check the row count by select count(*) from table3 where ID = %d

Why do I need to know the number of rows? Are these "rows" the row[0] and row[1] in the code? Thanks

You need the number of rows to check if your query had an emtpy result or not. And you need to check it to give your user an error message if the input value was invalid.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.