LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2012, 04:56 AM   #1
JeToMad
LQ Newbie
 
Registered: Mar 2012
Location: Valencia, Spain
Distribution: Fedora 15, Ubuntu 11.10
Posts: 15

Rep: Reputation: Disabled
CGI in C, problems when adding MySQL


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)

Last edited by JeToMad; 08-01-2012 at 05:07 AM. Reason: Adding info
 
Old 08-01-2012, 05:54 AM   #2
JeToMad
LQ Newbie
 
Registered: Mar 2012
Location: Valencia, Spain
Distribution: Fedora 15, Ubuntu 11.10
Posts: 15

Original Poster
Rep: Reputation: Disabled
Ok, I am really stupid. I was confused by some alias, and the Hello World example worked because the file was in both folders (The bad, ant the good one). So, as the second example was copied only to the bad folder, never worked.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
CGI MySql HTML Forms kantu Programming 2 02-26-2010 11:18 PM
[SOLVED] cgi/perl and mysql error Wim Sturkenboom Programming 6 02-13-2009 05:50 AM
/perl/php/cgi/mysql dramous Linux - Newbie 0 10-08-2004 05:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration