1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| #include <sys/time.h>
#include <stdio.h>
#include <mysql.h>
int main(char **args) {
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL *connection, mysql;
int state;
/* connect to the MySQL database at my.server.com */
mysql_init(&mysql);
connection = mysql_real_connect(&mysql,"my.server.com", 0, "db_test", 0, 0);
/* check for a connection error */
if (connection == NULL) {
/* print the error message */
printf(mysql_error(&mysql));
return 1;
}
state = mysql_query(connection,
"SELECT test_id, test_val FROM test");
if (state != 0) {
printf(mysql_error(connection));
return 1;
}
/* must call mysql_store_result() before can issue any other query calls */
result = mysql_store_result(connection);
printf("Rows: %d\n", mysql_num_rows(result));
/* process each row in the result set */
while ( ( row = mysql_fetch_row(result)) != NULL ) {
printf("id: %s, val: %s\n",(row[0] ? row[0] : "NULL"),(row[1] ? Row[1] : "NULL"));
}
/* free the result set */
mysql_free_result(result);
/* close the connection */
mysql_close(connection);
printf("Done.\n");
} |