I'm a student. Our final class project is the department website. My job is working on the Company Database, a db of company info and contacts that a professor has kept, and students use for job searches, etc. The server is Apache under Linux, a MySQL database, CGI's in C. We want to make it available on the website, registered users of course. I've got a certain amount of the HTML and CGI's working, and I'm pretty sure the connection and query code is OK here. But I've had problems with string functions to handle the result set from the queries, and in other CGI's I've had to avoid strcat. This one I decided to play with in the console, since compiling and running in the browser is such a pain. Right now I just want to test the idea of building a list of all the cities where there are companies in the db. If I query the db for all the cities in the city field, I get every instance of every city. I can get a list in the MySQL server itself, alphabetically, so Boston is listed a bunch of times. I figure I can do a while loop and use a temp string to see if the town has been added to the final output list yet. If not, it goes to printf("%s",row) and thus out to the HTML building the list for display.
Sounds good, but I get
Segmentation fault (core dumped)
In other CGI's, I would get "Internal Server Error" messages at times. Apparently either Apache or MySQL itself is choking on the code.
Help?#include #include #include #include
char select_string="select CPET2.Town from CPET2 order by Town;";
char * temp = NULL;
char * temprow;
"); // begin HTML
if (connection == NULL)
return 1 ;
state = mysql_query(connection,select_string);
if (state != 0)
result = mysql_store_result(connection);
while ((row = mysql_fetch_row(result)) != NULL);
temprow = row;
temp = row;
} // end main
I left the HTML stuff in, figuring it shouldn't cause a problem just getting printed to the screen.