mysql problem

From: Alfredo (alfredo_at_econtact.se)
Date: 09/23/04

  • Next message: Paul: "Re: calling fopen using function"
    Date: Thu, 23 Sep 2004 13:17:23 GMT
    
    

    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


  • Next message: Paul: "Re: calling fopen using function"

    Relevant Pages

    • RE: Recursive SQL query?
      ... I don't know if other database vendors support this. ... Subject: Recursive SQL query? ... You shouldn't really need to go recursing through a data ... key that references that same table's primary key. ...
      (perl.dbi.users)
    • RE: Expression Builder basic question on syntax / Or / how to incorpor
      ... it is always best to avoid unnecessary hits on the database. ... Use only the control name without the Me. ... runtime error on the textbox. ... This SQL query works, ...
      (microsoft.public.access.formscoding)
    • Re: Dave Peterson you help me with an userform could you help me again
      ... ps. you'd have to check to make sure res was a number, ... discrepancy with the database it will pop-up as an error. ... Dim ws As Worksheet ... Exit Sub ' Quit the macro ...
      (microsoft.public.excel.misc)
    • Re: Noob question on how to use an insert query in a command button
      ... in mm/dd/yyyy format (regardless of what your Short Date format have been ... Dim dbLocation 'Location of our Access database file ... Set objADO = CreateObject'Create an ADO connection ... objADO.Execute 'Execute this SQL query ...
      (microsoft.public.access.forms)
    • Re: cgi ordering a table with sql
      ... table, through the SQL query. ... often only values may be replaced by placeholders, ... the prepare should give the database ... implemented within the Perl layer instead of passing the prepared ...
      (perl.beginners)