/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>

#include "mysql.h"

int main()
{
    MYSQL *handle;
    MYSQL_RES *result;
    MYSQL_ROW row;
    int nfields;
    int *l;
    int i;
    handle= mysql_init(NULL);
    if(handle == NULL)
    {
        printf("MySQL error: %s", mysql_error(handle));
        exit(1);
    }
    if(!mysql_real_connect(handle, "localhost", "root", "", "Test", 0, NULL, 0))
    {
        printf("MySQL error: %s", mysql_error(handle));
        exit(1);
    }
    mysql_query(handle, "SELECT * FROM t1");
    result = mysql_store_result(handle);
    nfields = mysql_num_fields(result);
    /* */
    for(i=0; i<nfields; i++) {
        printf(" %s", mysql_fetch_field_direct(result, i)->name);
    }
    printf("\n");
    /* */
    while ((row = mysql_fetch_row(result))) {
        l = (int *)mysql_fetch_lengths(result);
        for (i=0; i<nfields; i++) {
            printf(" %.*s", row[i] ? l[i] : 4, row[i] ? row[i] : "NULL");
        }
        printf("\n");
    }
    mysql_free_result(result);
    mysql_close(handle);
    return 0;
}
