A
Alfredo
Hello everybody
I got a strange problem when retrieving data from a sql query. I have a
function that queries the database, gets data and store that in a struct. I
hope ist ok to post all the code:
ADINFO getRefPhone(char * ref){
ADINFO retInfo;
char * sqlQuery[100];
unsigned int num_fields;
MYSQL * conn;
MYSQL_RES * res;
MYSQL_FIELD * fd;
MYSQL_ROW row;
conn = mysql_init((MYSQL*) 0);
memset(&retInfo, 0, sizeof(retInfo));
if (conn!=NULL){
mysql_real_connect(conn, "0.0.0.0", "root", "", NULL, MYSQL_PORT,
NULL, 0);//the real ipnumber is not shown
if (conn!=NULL){
mysql_select_db(conn, "dbname");
strcpy(sqlQuery, "SELECT phone, user_id FROM reference WHERE
reference_id=");
strcat(sqlQuery, ref);
if (mysql_query(conn, sqlQuery)){
res = mysql_store_result(conn);
//HERE IS THE PROBLEM
if (res){
printf("resisok\n\r");
row = mysql_fetch_row(res);
if(row){
printf("rowisok\n\r");
strcpy(retInfo.phone, row[0]);
strcpy(retInfo.userid, row[1]);
}
else{
printf("rowisnotok\n\r");
}
}
else{
printf("resisnotok\n\r");
}
}
else{
printf("mysqlquereynotok\n\r");
}
mysql_close(conn);
}
else{
printf("connisnull\n\r");
}
}
else{
printf("connisnull\n\r");
}
return retInfo;
}
Everything works ok until I do
res = mysql_store_result(conn);
if (res)
Thus, res becomes null. Why?
The connection is ok. The query ( I printed it and it looks ok) too. The
char *ref contains the right value. I tried to print it put. eg. I got the
value 1010. I checked in the database and that value was actually stored in
the database. So please, why is res null?
Regards Alfredo
I got a strange problem when retrieving data from a sql query. I have a
function that queries the database, gets data and store that in a struct. I
hope ist ok to post all the code:
ADINFO getRefPhone(char * ref){
ADINFO retInfo;
char * sqlQuery[100];
unsigned int num_fields;
MYSQL * conn;
MYSQL_RES * res;
MYSQL_FIELD * fd;
MYSQL_ROW row;
conn = mysql_init((MYSQL*) 0);
memset(&retInfo, 0, sizeof(retInfo));
if (conn!=NULL){
mysql_real_connect(conn, "0.0.0.0", "root", "", NULL, MYSQL_PORT,
NULL, 0);//the real ipnumber is not shown
if (conn!=NULL){
mysql_select_db(conn, "dbname");
strcpy(sqlQuery, "SELECT phone, user_id FROM reference WHERE
reference_id=");
strcat(sqlQuery, ref);
if (mysql_query(conn, sqlQuery)){
res = mysql_store_result(conn);
//HERE IS THE PROBLEM
if (res){
printf("resisok\n\r");
row = mysql_fetch_row(res);
if(row){
printf("rowisok\n\r");
strcpy(retInfo.phone, row[0]);
strcpy(retInfo.userid, row[1]);
}
else{
printf("rowisnotok\n\r");
}
}
else{
printf("resisnotok\n\r");
}
}
else{
printf("mysqlquereynotok\n\r");
}
mysql_close(conn);
}
else{
printf("connisnull\n\r");
}
}
else{
printf("connisnull\n\r");
}
return retInfo;
}
Everything works ok until I do
res = mysql_store_result(conn);
if (res)
Thus, res becomes null. Why?
The connection is ok. The query ( I printed it and it looks ok) too. The
char *ref contains the right value. I tried to print it put. eg. I got the
value 1010. I checked in the database and that value was actually stored in
the database. So please, why is res null?
Regards Alfredo