Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL C API: How to work with row from mysql_fetch_row()?

Tags:

c++

c

mysql

This is some example code from my app:

int page_id;
string page_name;

enum COLUMNS {
    PAGE_ID,
    PAGE_NAME
};

if (mysql_query(conn, "SELECT page_id, page_name FROM pages")) {
    exit_mysql_error(conn);
}

MYSQL_RES *res = mysql_use_result(conn);

while (MYSQL_ROW row = mysql_fetch_row(res)) {
    page_id = *(int *)res[PAGE_ID];
    page_name = res[PAGE_NAME];
    // do some stuff where I need the page_id and page_name int/string
}

So imho this isn't the right way to obtain the integer value from the result (yet it works) but I couldn't find good example on how to do it. Also I'm quite new to programming in C++, so any feedback on the code sample is welcome.

like image 914
ddinchev Avatar asked Aug 13 '12 10:08

ddinchev


1 Answers

page_id = atoi(row[0]);
page_name = row[1]; // not really sure
like image 93
CyberDem0n Avatar answered Oct 16 '22 15:10

CyberDem0n