6

I'm confused when trying to fetch table rows in mysql using C++ with MySQL C API.

I can do it easily in PHP, just because C++ is a strongly-typed language so that we also need to take care of the dirty process..

This is how I done it in PHP

$data = array();
$i = 0;
$query = mysql_query("SELECT * FROM `my_table`");
while($fetch = mysql_fetch_array($query))
{
  $data[$i] = $fetch['columntobefetched'];
  $i++;
}

But how to do the same in C++ with MySQL API?

Here's my code so far....with a confusing dead end...x__x

   MYSQL *sqlhnd = mysql_init(NULL);
    mysql_real_connect(sqlhnd, "server", "user", "pass", "database", port, NULL, 0);

    mysql_query(sqlhnd, "SELECT * FROM `my_table`");
    MYSQL_RES *confres = mysql_store_result(sqlhnd);
    int totalrows = mysql_num_rows(confres);
    int numfields = mysql_num_fields(confres);
    MYSQL_FIELD *mfield;

    while((row = mysql_fetch_row(confres)))
    {
        for(i = 0; i < numfields; i++)
        {
            while(mfield = mysql_fetch_field(confres))
            {
                mfield->//??? I'm dead
            }
        }
    }

Basically I wanted to get a value from a field in the database table and store it to a variable..

Any kind of help would be appreciated :)

Thanks

2 Answers 2

15

In the MySQL C API, mysql_fetch_row returns a MYSQL_ROW object, which is essentially an array of values in the current row.

So, your code should be something like:

mysql_query(sqlhnd, "SELECT * FROM `my_table`");
MYSQL_RES *confres = mysql_store_result(sqlhnd);
int totalrows = mysql_num_rows(confres);
int numfields = mysql_num_fields(confres);
MYSQL_FIELD *mfield;

while((row = mysql_fetch_row(confres)))
{
    for(i = 0; i < numfields; i++)
    {
        char *val = row[i];
        // do something with val...
    }
}

Better yet, don't do a "SELECT * FROM mytable" in a program. It would be much better to name the fields you expect, so that you can be sure of the order of the fields returned.

Sign up to request clarification or add additional context in comments.

2 Comments

What does the abbreviation "conf" stand for in your variable names?
This will leak memory. "You must also call mysql_free_result() after you are done with the result set." dev.mysql.com/doc/refman/5.7/en/mysql-store-result.html See also dev.mysql.com/doc/refman/5.7/en/c-api-function-overview.html
4

If you are using C++ why not using MySQL++? This is some sample code:

mysqlpp::Connection dbconnection;
dbconnection.connect("database", "server", "user", "pass");

mysqlpp::Query prepared_query = dbconnection.query( "SELECT * FROM `my_table`" );

mysqlpp::StoreQueryResult r = prepared_query.store();

int field = r.field_num("columntobefetched");

for(mysqlpp::StoreQueryResult::iterator i = r.begin(); i!=r.end();i++)
{
      std::cout << i->at(field) << std::endl;
      std::cout << (*i)["columntobefetched"] << std::endl; // this will be slower
}

2 Comments

Also (by default) the c++ connector code throws exceptions if something fails. Since your example C code doesn't have any error handling at all, you might be interested in that feature.
Nit: you need double quotes around "columntobefetched"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.