/* to compile: gcc -g -I/path/to/mysql-4.1/include psbug.c -L/path/to/mysql-4.1/libmysql/.libs \ -lmysqlclient -lz -lm -lssl -lcrypto -lcrypt */ #include #include #include #include #include #define myerror(msg) print_error(msg) #define mysterror(stmt, msg) print_st_error(stmt, msg) MYSQL mysql; static void print_error(const char *msg) { if (&mysql && mysql_errno(&mysql)) { if (&mysql.server_version) fprintf(stdout, "\n [MySQL-%s]", &mysql.server_version); else fprintf(stdout, "\n [MySQL]"); fprintf(stdout, "[%d] %s\n", mysql_errno(&mysql), mysql_error(&mysql)); } else if (msg) fprintf(stderr, " [MySQL] %s\n", msg); } static void print_st_error(MYSQL_STMT *stmt, const char *msg) { if (stmt && mysql_stmt_errno(stmt)) { if (stmt->mysql && stmt->mysql->server_version) fprintf(stdout, "\n [MySQL-%s]", stmt->mysql->server_version); else fprintf(stdout, "\n [MySQL]"); fprintf(stdout, "[%d] %s\n", mysql_stmt_errno(stmt), mysql_stmt_error(stmt)); } else if (msg) fprintf(stderr, " [MySQL] %s\n", msg); } int main(int argc, char **argv) { MYSQL_STMT * stmt; MYSQL_RES * result; MYSQL_FIELD * fields; char * data; MYSQL_BIND bind; unsigned long length; my_bool is_null; unsigned int i,col_type; int rc; char * statement = "select repeat('a',80000)"; mysql_init(&mysql); mysql_real_connect(&mysql,"localhost","root","","test",0,NULL, 0); if (!(stmt = mysql_stmt_init(&mysql))) { myerror("Can't init stmt\n"); } if (mysql_stmt_prepare(stmt, statement, strlen(statement))) { mysterror(stmt, "Can't prepare stmt\n"); } if (mysql_stmt_execute(stmt)) { mysterror(stmt, "Can't execute stmt\n"); } if (!(result = mysql_stmt_result_metadata(stmt))) { mysterror(stmt, "Can't get metadata\n"); } fields = mysql_fetch_fields(result); col_type = fields ? fields[0].type : MYSQL_TYPE_STRING; printf("TYPE %d LEN %d \n", fields[0].type, fields[0].length); bind.buffer_type= col_type; bind.buffer_length= fields[0].length; bind.length = &(length); bind.is_null= &(is_null); bind.buffer = (char *) data; if (!(data=malloc(bind.buffer_length))) { myerror("Can't allocate memory for data\n"); } if (mysql_stmt_bind_result(stmt, &bind)) { mysterror(stmt, "Can't bind result\n"); } if ((rc = mysql_stmt_fetch(stmt))) { if (rc == 1) { mysterror(stmt, "Can't fetch result\n"); } if (rc == 100) { mysterror(stmt, "End of Data\n"); } } if ( length > bind.buffer_length ) { bind.buffer_length= length; free(data); if (!(data= malloc(length))) { myerror("Can't allocate memory"); } if (mysql_stmt_fetch_column(stmt, &bind , 0, 0 )) { mysterror(stmt, "Can't do mysql_stmt_fetch_column()\n"); } } printf("DATA - %s\n", data); mysql_stmt_close(stmt); mysql_close(&mysql); return 0; }