> Any pointers on where to look?

That line says it all really.  Your initiallizing connection as a pointer
(denoted by *), you have to treat it accordingly when passing it to
functions.  Your function init would look something like this:

MYSQL_RES doquery (MYSQL *connection, char query[100]) {
    int state;
    MYSQL_RES *result;
    state = mysql_query(connection,query);
    if (state != 0) {
        printf(mysql_error(connection));
        return NULL;
    }
    result = mysql_store_result(connection);
    return &result;
}

then, in your code, you would connect like normal, then call the function
like this:
MYSQL_RES *result;

result = doquery(&connection, query);
----- Original Message -----
From: "Seth Northrop" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Monday, May 21, 2001 12:25 PM
Subject: C API; queries within functions.


>
> Hi!
>
> This is probably more of a C inadequacy than a MySQL problem.  I'm playing
> around with C with MySQL (I have to date only interfaced with it in
> PHP) and I was curious if anyone had any example code which illustrates
> how you would write functions which return mysql data.
>
> For example, in PHP I might write a simple function  like this:
>
> function get_specific_db_column($dbname, $tblname, $pk,
>                                 $pk_val, $colname)
> {
>
>   $sql = "SELECT $colname as val
>             FROM $dbname.$tblname
>            WHERE $pk = '$pk_val'";
>   $r = mysql_query($sql);
>   while ($row = mysql_fetch_array($r))
>     $array[] = $row;
>   return $array;
>
> }
>
> But, in C I haven't figured out how to do things like navigate passing the
> MYSQL *mysql init pointer into the function for mysql_query etc.
>
> I can however get a program WITHOUT functions (ie, hardcode the
> query) compiled and working:
>
> // Simple test application
> // for the C Mysql API
>
> #include <sys/time.h>
> #include <stdio.h>
> #include <mysql.h>
>
> #define def_host_name NULL
> #define def_user_name "root"
> #define def_password  NULL
> #define def_db_name "reflectivity"
>
> int main (char **arg)
> {
>
>   MYSQL_RES *result;
>   MYSQL_ROW row;
>   MYSQL *connection, mysql;
>   int state;
>   char colsize=10, rowsize=10;
>
>   // connect to the mysql database on internal
>   mysql_init(&mysql);
>   connection = mysql_real_connect(&mysql, def_host_name,
>                      def_user_name,
>                      def_password,
>                      def_db_name,
>                      0,             /*port defaut*/
>                      NULL,          /*socket default*/
>                      0);            /*flag*/
>   if (connection == NULL) // check for a connection error
>   {
>     // print the error message
>     printf(mysql_error(&mysql));
>     return 1;
>   }
>   state = mysql_query(connection,"SELECT * from reflectivity.accounts");
>   if (state != 0)
>   {
>     printf(mysql_error(connection));
>     return 1;
>   }
>   // you must call mysql_store_result before we can issue anything else
>   result = mysql_store_result(connection);
>   printf("Rows: %d\n", mysql_num_rows(result));
>   // process each row in the result set
>   while ((row = mysql_fetch_row(result)) != NULL)
>     printf("%s - %s - %s - %s - %s\n",row[0],row[1],row[2],row[3],row[4]);
>   // free some memory
>   mysql_free_result(result);
>   // close the mysql connection
>   mysql_close(connection);
>   printf("Done.\n");
> }
>
> Ny guess is that if I just see a couple of examples which mimic some of
> the functionality of the above PHP function in C I'll be able to write
> them without a problem.
>
> Any pointers on where to look?
>
> Thanks!
> Seth
>



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to