mysql problem

Discussion in 'C Programming' started by Alfredo, Sep 23, 2004.

  1. Alfredo

    Alfredo Guest

    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
    Alfredo, Sep 23, 2004
    #1
    1. Advertising

  2. "Alfredo" <> wrote in
    news:D1A4d.3950$:

    > 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.

    --
    - Mark ->
    --
    Mark A. Odell, Sep 23, 2004
    #2
    1. Advertising

  3. Alfredo

    Fatted Guest

    Alfredo wrote:

    <snip>

    > 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...
    Fatted, Sep 23, 2004
    #3
  4. Alfredo

    Default User Guest

    Alfredo wrote:

    > Hello everybody
    >
    > I got a strange problem when retrieving data from a sql query.



    comp.databases



    Brian Rodenborn
    Default User, Sep 23, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JL
    Replies:
    0
    Views:
    1,142
  2. Ravi
    Replies:
    6
    Views:
    1,406
    Suchandra Thapa
    Jul 21, 2003
  3. Replies:
    2
    Views:
    6,189
  4. washakie
    Replies:
    4
    Views:
    922
    washakie
    Jan 15, 2008
  5. Jeffrey H. Coffield
    Replies:
    1
    Views:
    1,848
Loading...

Share This Page