mysql problem

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
 
M

Mark A. Odell

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.

Wouldn't a sql newsgroup seem more appropriate for this question? Just
because you write something in C doesn't mean your question is about the
*C* language. You have a question about a SQL function - not C.
 
F

Fatted

Alfredo wrote:

Everything works ok until I do
res = mysql_store_result(conn);

if (res)

Thus, res becomes null. Why?

Check out:

http://dev.mysql.com/doc/mysql/en/mysql_store_result.html

<quote>
mysql_store_result() also returns a null pointer if reading of the
result set failed. You can check whether an error occurred by checking
if mysql_error() returns a non-empty string, if mysql_errno() returns
non-zero, or if mysql_field_count() returns zero.

An empty result set is returned if there are no rows returned. (An empty
result set differs from a null pointer as a return value.)
</quote>

Some additional debugging code may be required...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top