Skip to content

Instantly share code, notes, and snippets.

@lierdakil
Last active January 28, 2021 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lierdakil/66575a13b70684ab1e49fc7903e6005b to your computer and use it in GitHub Desktop.
Save lierdakil/66575a13b70684ab1e49fc7903e6005b to your computer and use it in GitHub Desktop.
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#define finish_with_error(con) { \
fprintf(stderr, "%d: %s\n", __LINE__, mysql_error(con));\
exit(1); }\
int main() {
MYSQL db;
MYSQL* con = &db;
mysql_init(con);
mysql_real_connect(con, "dbserver", "pdns", "pass", "pdns", 3306, NULL, CLIENT_MULTI_RESULTS);
if(mysql_ping(con)) finish_with_error(con);
// this one breaks
if(mysql_query(con, "select d.id, d.name, d.notified_serial, r.content from records r join domains d on r.name=d.name where r.type='SOA' and r.disabled=0 and d.type='MASTER'")) finish_with_error(con);
// but this one works
//if(mysql_query(con, "select d.id, d.name, d.notified_serial, r.content from records as r join domains as d on r.name=d.name where r.type='SOA' and r.disabled=0 and d.type='MASTER'")) finish_with_error(con);
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) finish_with_error(con);
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
mysql_close(con);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment