Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Last active September 16, 2016 09:50
Show Gist options
  • Select an option

  • Save bitsmanent/09e4ed2864b02f79e5af80b52e925f6e to your computer and use it in GitHub Desktop.

Select an option

Save bitsmanent/09e4ed2864b02f79e5af80b52e925f6e to your computer and use it in GitHub Desktop.
Run a parametrized MySQL query (printf()-like).
MYSQL_RES *
mysql_exec(const char *sqlstr, ...) {
MYSQL_RES *res;
va_list ap;
char *sql;
int sqlen;
va_start(ap, sqlstr);
sqlen = vasprintf(&sql, sqlstr, ap);
va_end(ap);
if(mysql_real_query(mysql, sql, sqlen)) {
free(sql);
return NULL;
}
free(sql);
res = mysql_store_result(mysql);
if(!res)
return NULL;
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment