Jeff wrote:
> Hello all,
>
> I am trying to write a simple cgi that will read a postgresql database
> (just a list of names for now)and print it. I am not having any luck. I
> have attached it, and cgi-lib.c and .h, that have some functions for
> parsing the query string, etc. If someone has some time, some help would
> be great.
> FieldArray *fa;
This should be an actual FieldArray, not a pointer to one.
The compiler should have complained about this, e.g.:
namelist.c:17: warning: passing arg 1 of `ReadParse' from incompatible pointer type
namelist.c:18: warning: passing arg 1 of `GetValue' from incompatible pointer type
This alone may prevent the program from working.
You should also provide ANSI prototypes for your own functions. This
will help to catch errors.
> if(PQstatus(&conn) == CONNECTION_BAD)
> {
> printf("%s",PQerrormessage(conn));
Error messages should be writting to stderr rather than to stdout.
This is particularly important for CGI programs, as:
a) you usually want the error to show up in your error log, rather
than being sent to the client, and
b) if the error is written to stdout here, it will occur before the
`Content-Type:' header that PrintHeader() writes, which will confuse
httpd.
Also if the output is declared to be text/html, anything that you send
needs to be formatted appropriately (e.g. `<', `>' and `&' replaced
with `<', `>' and `&'
> totalnames = (int *)PQntuples(res);
> struct fullnames fullnameslist[50];
Better to use:
struct fullnames *fullnameslist =
alloca(totalnames * sizeof(struct fullnames));
so that you don't have to worry about overflowing the array.
> fullnameslist[i].first = (char *)PQgetvalue(res, i, 0);
> fullnameslist[i].last = (char *)PQgetvalue(res, i, 1);
This is wrong. The `first' and `last' fields are of type `char[]', not
`char *'. You will either need to use:
strncpy(fullnameslist[i].first, (char *)PQgetvalue(res, i, 0), 50);
strncpy(fullnameslist[i].last, (char *)PQgetvalue(res, i, 1), 50);
or (better still), redefine `struct fullnames' as
struct fullnames {
char *first;
char *last;
};
and use:
fullnameslist[i].first = strdup((char *)PQgetvalue(res, i, 0));
fullnameslist[i].last = strdup((char *)PQgetvalue(res, i, 1));
> for(i=0; i<totalnames; i++)
> {
> printf("<BR>%s %s<BR>",fullnameslist[1].first, fullnameslist[1].last);
> }
Shouldn't this refer to fullnameslist[i]?
--
Glynn Clements <[EMAIL PROTECTED]>