Need help in executing Joins using C API for Mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • just4priya
    New Member
    • Sep 2007
    • 19

    Need help in executing Joins using C API for Mysql

    Hi,

    I am executing the following query

    [CODE=mysql]select * from mgroup m RIGHT JOIN cgroup c on (c.m_id=m.gid) where m.owner='priya' and c.user='appu'; [/CODE]

    Then i get the following result

    +------+---------+-------+------+------+
    | gid | grpname | owner | m_id | user |
    +------+---------+-------+------+------+
    | 3 | family | priya | 3 | appu |
    +------+---------+-------+------+------+


    However if i write the same query using C API, it gives me output as
    "0 rows returned"

    C Code snippet:

    [CODE=c]sprintf(stmt_bu f,"select * from mgroup m RIGHT JOIN cgroup c on (c.m_id=m.gid) where m.owner='%s' and c.user='%s'",db recowner,dbrecu ser);

    if (mysql_query(co nn,stmt_buf))
    {
    fprintf(stderr, "%s\n", mysql_error(con n));
    return 0;
    }

    res = mysql_store_res ult(conn);

    if(res == NULL)
    {
    return 0;
    }
    else
    {
    /* process result set, and then deallocate it */
    process_result_ set (conn, res);
    }
    ....
    ...
    }

    void process_result_ set (MYSQL *conn, MYSQL_RES *res_set)
    {

    unsigned int i;
    while ((row = mysql_fetch_row (res_set)) != NULL)
    {
    for (i = 0; i <= mysql_num_field s (res_set); i++)
    {
    if (i > 0)
    fputc ('\t', stdout);
    printf ("%s", row[i] != NULL ? row[i] : "NULL");
    }
    fputc ('\n', stdout);
    }
    if (mysql_errno (conn) != 0)
    fprintf(stderr, "%s\n",conn , mysql_errno (conn));
    else
    printf ("%lu rows returned\n",
    (unsigned long) mysql_num_rows (res_set));

    }[/CODE]


    Please provide ur suggestions.
    Thanks in advance

    ~Priya
    Last edited by mwasif; Dec 5 '07, 01:39 PM. Reason: Added code tags.
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    This is not possible that the same query generates different results. To identify the problem make sure that you are connected to the correct database. Print the query in your code before sending it to MySQL. This will make sure you are executing the correct query.

    Comment

    • just4priya
      New Member
      • Sep 2007
      • 19

      #3
      Originally posted by mwasif
      This is not possible that the same query generates different results. To identify the problem make sure that you are connected to the correct database. Print the query in your code before sending it to MySQL. This will make sure you are executing the correct query.
      Ya i verified that i m connected to same database.
      My all other queries such as add, search is working fine.
      Only the query with a Right Join isnt working..

      Please could you suggest where i m going wrong

      Thanks in advance

      Comment

      • just4priya
        New Member
        • Sep 2007
        • 19

        #4
        The Join query worked successfully using the above method.

        Comment

        Working...