Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Return a result from a MySQL query in a function after closing connection in C
- int CheckBox(char *mac)
- {
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- /* database details */
- char *server = "localhost";
- char *user = "user";
- char *password = "pw";
- char *database = "db";
- int retvalue;
- conn = mysql_init(NULL);
- /* connect to database */
- if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit(1);
- }
- /* create and send SQL query */
- char query[1600];
- sprintf(query,"SELECT * FROM boxes WHERE mac = '%s'", mac);
- if (mysql_query(conn, query))
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit(1);
- }
- res = mysql_use_result(conn);
- /* check result to see if we have a hit */
- if ((row = mysql_fetch_row(res)) != NULL)
- {
- printf("mac (%s) did exist with id %s.n",mac,(char *)row[0]);
- retvalue = (int)row[0];
- } else
- {
- printf("mac (%s) did NOT exist ",mac);
- /* mac address did not yet exist, so create it */
- sprintf(query,"INSERT INTO boxes (mac) VALUES ('%s')",mac);
- if (mysql_query(conn, query))
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit(1);
- } else
- {
- printf("but now doesn");
- CheckBox(mac);
- }
- }
- /* close connection */
- mysql_free_result(res);
- mysql_close(conn);
- /* return id number of box */
- return retvalue;
- }
Add Comment
Please, Sign In to add comment