Example

int main(int argc, char *argv[])
{
  MYSQL_RES *res;
  unsigned int fields;
  my_ulonglong rows;
  MYSQL_ROW row;

  connect("127.0.0.1", "root", "");

  res=query("SELECT * FROM `clients`");

  fields=mysql_field_count(&connection); //<<1

  rows=mysql_num_rows(res); //<<2


  for (int i=0; i < rows; i++)
    {
        unsigned long *lengths;

        row=mysql_fetch_row(res); //<<3

        lengths = mysql_fetch_lengths(res); //<<4

        for (int fnr=0; fnr < fields; fnr++)
            printf("[%i: %s] ", (int) lengths[fnr],
                    row[fnr] ? row[fnr] : "NULL"); //<<5
    }

  mysql_free_result(res); //<<6

  disconnect();

  return EXIT_SUCCESS;
}
  1. Get the number of fields of the table.
  2. Get the amount of result rows.
  3. Fetch a row (one by one) and store the result in row.
  4. Store the lengths (in characters) of the result fields.
  5. Print the fields of one row.
  6. Free the memory by deleting the result data.



root 2015-09-06