6 struct used_result *next;
9 struct escaped_string {
11 struct escaped_string *next;
14 MYSQL *mysql_conn = NULL;
15 static struct used_result *used_results;
16 static struct escaped_string *escaped_strings;
19 if(mysql_ping(mysql_conn)) {
24 MYSQL_RES *mysql_use() {
25 MYSQL_RES *res = mysql_use_result(mysql_conn);
26 struct used_result *result = malloc(sizeof(*result));
28 mysql_free_result(res);
32 result->next = used_results;
33 used_results = result;
38 struct used_result *result, *next_result;
39 for(result = used_results; result; result = next_result) {
40 next_result = result->next;
41 mysql_free_result(result->result);
44 struct escaped_string *escaped, *next_escaped;
45 for(escaped = escaped_strings; escaped; escaped = next_escaped) {
46 next_escaped = escaped->next;
47 free(escaped->string);
53 mysql_conn = mysql_init(NULL);
54 if (!mysql_real_connect(mysql_conn, MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_BASE, MYSQL_PORT, NULL, 0)) {
61 mysql_close(mysql_conn);
64 void show_mysql_error() {
66 printf("MySQL Error: %s\n", mysql_error(mysql_conn));
69 void printf_mysql_query(const char *text, ...) {
71 char queryBuf[MYSQLMAXLEN];
74 va_start(arg_list, text);
75 pos = vsnprintf(queryBuf, MYSQLMAXLEN - 2, text, arg_list);
77 if (pos < 0 || pos > (MYSQLMAXLEN - 2)) pos = MYSQLMAXLEN - 2;
79 printf("MySQL: %s\n", queryBuf);
80 if(mysql_query(mysql_conn, queryBuf)) {
85 char* escape_string(const char *str) {
86 struct escaped_string *escapedstr = malloc(sizeof(*escapedstr));
90 char escaped[strlen(str)*2+1];
91 mysql_real_escape_string(mysql_conn, escaped, str, strlen(str));
92 escapedstr->string = strdup(escaped);
93 escapedstr->next = escaped_strings;
94 escaped_strings = escapedstr;
95 return escapedstr->string;