…ere bound to a statement handle If the statement handle was previously used, and so mysql_stmt_bind_result was called, and if that result set and bind buffers were freed, MySQL still thinks the result set buffer is available and will prefetch the first result in mysql_stmt_execute. This will corrupt or crash the program. By setting bind_result_done back to 0, we make MySQL think that a result set has never been bound to this statement handle before to prevent the prefetch. Is this a MySQL bug or a problem in the workflow, i.e. should a result buffer be allocated and bound once and then never "unbound" from a statement handle?
This prevents the GC from getting the Statement first, which would call mysql_stmt_close, which calls mysql_stmt_result_free on any outstanding results. When the Result object gets GC next, it also calls mysql_stmt_result_free and that would crash.
Where a simple nul-terminated C string is required, use StringValueCStr. Where a string pointer and length are used, follow the pattern: Check_Type(str, T_STRING); ptr = RSTRING_PTR(str); len = RSTRING_LEN(str);