Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7896857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:00:05+00:00 2026-06-03T08:00:05+00:00

I have this weird behavior in C which I do not understand. It is

  • 0

I have this weird behavior in C which I do not understand. It is probably me as I am fairly new to C. What I am trying to achieve is to write a function that runs a MySQL query and returns the result. The weird thing is that, in the code below, if I close the connection and free the result (as I would expect the order to be) it seems that the value that I have stored in ‘retvalue’ is no longer there. When I put the return before the freeing of the result and closing of the connection the value is returned, but obviously the connection is not closed and MySQL will eventually present the error ‘too many connections’.

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, "%s\n", 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, "%s\n", 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, "%s\n", mysql_error(conn));
        exit(1);
    } else
    {
        printf("but now does\n");
        CheckBox(mac);
    }
}

/* close connection */
mysql_free_result(res);
mysql_close(conn);

/* return id number of box */
return retvalue;
}

So the questions are:
Am I right to assume that first freeing the result and closing the connection is the right order?
and why is the value that I have assigned to retvalue gone when I run the function like this?

Thanks for being nice to a beginner 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:00:07+00:00Added an answer on June 3, 2026 at 8:00 am

    The cast retvalue = (int)row[0] is wrong. It puts the address of the result (converted to int) in retvalue. row[0] is a char*, so you should use retvalue=atoi(row[0]).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am unable to understand the weird behavior of this program. I have 2
This is the day of weird behavior. We have a Win32 project made with
This could be weird, Have you ever come across a blog which you wanted
This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
This is very weird. I have the following code: Assert.AreEqual(new DateTime(2000, 1, 1), DateTime.ParseExact(2000,
I have a network with some weird (as I understand) DNS server which causes
Having a weird issue. I'm new to Macs and have a windows VM that
I`m really confused here about this weird behavior! The thing is, I have a
I witnessed the following weird behavior. I have two functions, which do almost the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.