0

I retrieve rows via C API.

MYSQL_ROW row;
int i=0;
char* A[100];
while ((row = mysql_fetch_row(result))){
    A[i]=row; // My Question
    i++;
}

mysql_free_result(result);
mysql_close(con);

for(int i=0;i<sizeof(A);i++){
    printf("%s\n",A[1]);
}

How can I save the entire rows in an array, independent of MySQL connection?

1
  • 1
    You need to copy the data. Assuming that MYSQL_ROW is an alias for char *, then you just copy the pointer itself, not the data it actually points to. Commented Jul 1, 2021 at 13:36

1 Answer 1

2

You have to duplicate the field value.

For example, duplicating only the first field:

#define MAX_REC 100
MYSQL_ROW row;
unsigned int i;
char* A[MAX_REC];

i = 0;
while ((i < MAX_REC) && (row = mysql_fetch_row(result))) {
    // Copy first field only (Create a duplicate that must be freed later)
    A[i] = row[0] ? strdup(row[0]) : "NULL";
    i++;
}

If you need all fields, an inner loop and a 2D array is necessary. Don't forget to free all duplicated values when no more needed!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.