Greetings,
Once again this big newbie comes to be enlightened by the great programmers here.
I have just started programming CGI. In fact, I need just a very simple thing: I have a MySQL DB, which contains a list of names and timestamps. I thought that would be easy to do a simple program that show the timestamp for a given name.
I started from the very begining:
--> Hello world example in C: compiled, then I put in "/var/www/cgi-bin" and opened a browser: localhost/cgi-bin/HW.cgi shown correctly.
Then, I took a working C program that looks on the DB and shows values in the command line. Added some lines in order to output html instead of plain text. Compiled ok. Copied to the same folder. but now keeps complaining with 404 error. Checked path, renamed the file, checked permissions... nothing seems to work. May it be related to the MySQL part? I am lost here, and was only meant to be the first test (Just shows the whole DB, and the query string).
Anyways, here is the code (I suppose has nothing to do since the error is 404, but...)
[There are 5 values because of the "name"is divided amongst 4 strings]
Code:
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "***"; /* it's ok in the real code ;) */
char *password = "***"; /* it's ok in the real code ;) */
char *database = "test_db";
conn = mysql_init(NULL);
char *query_cgi;
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
if (mysql_query(conn, "SELECT * FROM estado")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
printf("Content-type: text/html\n\n");
printf("<html>\n");
printf("<body>\n");
printf("<hl>Valores actualmente en la tabla:<hl>");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s %s %s %s %s<hl>", row[0], row[1], row[2], row[3], row[4]);
query_cgi = getenv("QUERY_STRING");
printf("<hl>La query, por otra parte, era %s <hl>\n", query_cgi);
printf("</body>\n");
printf("</html>\n");
mysql_free_result(res);
mysql_close(conn);
return 0;
}
As always, please forgive my lack of skill when writing in English. Hope is good enough to be understood...
Thanks in advance guys!
EDIT: I forgot to say that, executed from the command line, program works ok (just a null query, but the DB values are shown correctly)