E
Eli Criffield
I'm getting odd results from my fuction that takes variable aguments.
What i want to do is take a printf like format and pass that to
mysql_query then i do some processing of the results and return a
pointer to an array of the results.
Everything works if i guess at the length of the query and malloc that
amount. The problem comes when i try to figure out the size of the
varible arguments.
The way i figure this out below is taking directly from FAQ question
15.4 and 15.5.
I also need to be able to tell the size lquery needs to be even if i
pass it an int or float or whatever, not just strings, is that possible
?
What am i doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char ***simplesql_query(int *conn, unsigned int *NumberOfRows, unsigned
int *NumberOfFields, char *query, ...)
{
/* parse the query using printf like format and put that all in
lquery */
va_list argp;
char *lquery;
size_t len;
char *p;
/* figure out the size of the query */
va_start(argp, query);
len = strlen(query);
printf("query=%s:len=%d\n",query,len);
while((p = va_arg(argp, char *)) != NULL)
{
/* p has some odd stuff in it, why???? */
printf("len is %d,p is %s\n",len,p);
len += strlen(p);
}
va_end(argp);
/* add one for \0 */
len++;
if ((lquery = malloc(len)) == NULL)
return NULL;
/* pass off query to vsprintf */
va_start(argp, query);
vsprintf(lquery, query, argp);
va_end(argp);
printf("simplesql_query: %s, lentgh=%d\n", lquery,len);
/*
do mysql work stuff
if (mysql_query(conn, lquery) != 0)
simplesql_error(conn,"SELECT failed");
else
...
*/
return NULL;
}
int main()
{
int *conn = 0;
char ***sqlresults;
int rows, fields;
/* odd results */
char userid2[] = "5";
sqlresults = simplesql_query(conn,&rows,&fields,"SELECT UserID FROM
Users WHERE UserID < %s",userid2);
return 0;
}
What i want to do is take a printf like format and pass that to
mysql_query then i do some processing of the results and return a
pointer to an array of the results.
Everything works if i guess at the length of the query and malloc that
amount. The problem comes when i try to figure out the size of the
varible arguments.
The way i figure this out below is taking directly from FAQ question
15.4 and 15.5.
I also need to be able to tell the size lquery needs to be even if i
pass it an int or float or whatever, not just strings, is that possible
?
What am i doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char ***simplesql_query(int *conn, unsigned int *NumberOfRows, unsigned
int *NumberOfFields, char *query, ...)
{
/* parse the query using printf like format and put that all in
lquery */
va_list argp;
char *lquery;
size_t len;
char *p;
/* figure out the size of the query */
va_start(argp, query);
len = strlen(query);
printf("query=%s:len=%d\n",query,len);
while((p = va_arg(argp, char *)) != NULL)
{
/* p has some odd stuff in it, why???? */
printf("len is %d,p is %s\n",len,p);
len += strlen(p);
}
va_end(argp);
/* add one for \0 */
len++;
if ((lquery = malloc(len)) == NULL)
return NULL;
/* pass off query to vsprintf */
va_start(argp, query);
vsprintf(lquery, query, argp);
va_end(argp);
printf("simplesql_query: %s, lentgh=%d\n", lquery,len);
/*
do mysql work stuff
if (mysql_query(conn, lquery) != 0)
simplesql_error(conn,"SELECT failed");
else
...
*/
return NULL;
}
int main()
{
int *conn = 0;
char ***sqlresults;
int rows, fields;
/* odd results */
char userid2[] = "5";
sqlresults = simplesql_query(conn,&rows,&fields,"SELECT UserID FROM
Users WHERE UserID < %s",userid2);
return 0;
}