1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "mysql.h"
5
6 MYSQL my_connection;
7 MYSQL_RES *res_ptr;
8 MYSQL_ROW sqlrow;
9
10 void display_row()
11 {
12 unsigned int field_count;
13 field_count = 0;
14 while(field_count < mysql_field_count(&my_connection))
15 {
16 printf("%s ",sqlrow[field_count]);
17 field_count++;
18 }
19 printf("
");
20 }
21 int main(int argc, char *argv[])
22 {
23 int res;
24
25 mysql_init(&my_connection);
26 if(mysql_real_connect(&my_connection,"localhost","rick","secret","foo",0,NULL,0))
27 {
28 printf("Connection success
");
29 res = mysql_query(&my_connection,"select childno, fname,age from children where age > 5");
30 if(res)
31 {
32 printf("select error:%s
",mysql_error(&my_connection));
33 }
34 else
35 {
36 res_ptr = mysql_store_result(&my_connection);
37 if(res_ptr)
38 {
39 printf("Retrieved %lu rows
",(unsigned long)mysql_num_rows(res_ptr));
40 while((sqlrow = mysql_fetch_row(res_ptr)))
41 {
42 printf("Fetched data...
");
43 display_row();
44 }
45 if(mysql_errno(&my_connection))
46 {
47 fprintf(stderr,"Retrive error:%s
",mysql_error(&my_connection));
48 }
49 mysql_free_result(res_ptr);
50 }
51 }
52 mysql_close(&my_connection);
53 }
54 else
55 {
56 fprintf(stderr,"Connection failed
");
57 if(mysql_errno(&my_connection))
58 {
59 fprintf(stderr,"Connection error %d:%s
",mysql_error(&my_connection));
60 }
61 }
62 return EXIT_SUCCESS;
63 }