Mysql CGi und C++ problemchen



  • Hallole,
    bin grade dabei meine ersten Schritte mit C und CGI zu machen.
    Jetzt hab ich mal ein paar examples zusammengesucht und versucht ne Verbindung zur DB herzustellen. Das funktioniert auch wenn ich das ganze als Konsolenanwendung und .exe starte.
    Dann hab ich die Datei in .cgi umbenannt und versuche sie dann auszuführen, dann bekomm ich vom Apache ne Fehlermeldung.

    Hier mal der Source

    #include <stdio.h>
    #include <windows.h>
    #include <mysql/mysql.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        MYSQL_RES *mTabelle;
        MYSQL_ROW mRecord;
        MYSQL *mConnection, mDB;
        int mError;
        mysql_init(&mDB);
        mConnection = mysql_real_connect(&mDB,"localhost","root","","test",0,0,0);
        if (mConnection == NULL)  
        {
            printf("Es konnte keine Verbindung zur Datenbank hergestellt werden: %s",mysql_error(&mDB));
            return 1;
        } 
        else 
        {
         printf("Es wurde erfolgreich eine Verbindung hergestellt!");
        }
        mysql_close(mConnection);
    
        //system("PAUSE");
        return EXIT_SUCCESS;
    }
    

    Und mein Apache-Fehler:

    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>500 Internal Server Error</title>
    </head><body>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error or
    misconfiguration and was unable to complete
    your request.</p>
    <p>Please contact the server administrator,
    admin@localhost and inform them of the time the error occurred,
    and anything you might have done that may have
    caused the error.</p>
    <p>More information about this error may be available
    in the server error log.</p>
    <p>Additionally, a 500 Internal Server Error
    error was encountered while trying to use an ErrorDocument to handle the request.</p>
    <hr>
    <address>Apache/2.2.0 (Win32) DAV/2 mod_ssl/2.2.0 OpenSSL/0.9.8a mod_autoindex_color PHP/5.1.1 Server at localhost Port 80</address>
    </body></html>

    Kann mir da jemande bitte helfen? Danke.



  • Ups, manchmal sollte man halt erst denken 😉

    Hab den Fehler schon gefunden, das ganze ist ja CGI
    Jetzt gehts.

    #include <cstdlib>
    #include <iostream>
    #include <time.h>
    #include <stdio.h>
    #include <windows.h>
    #include <mysql/mysql.h>
    
    using namespace std;
    /* Die Kopfzeile eines Standard-HTML-Dokuments
     * Titel: String, der als Titel erscheinen soll
     */
    void print_html_header(char *titel)
    {
       printf("<html><head>\n");
       printf("<title>%s</title>\n",titel);
       printf("</head><body>\n");
    }
    
    /* Das Ende eines HTML-Dokuments */
    void print_html_end()
    {
       printf("</body></html>\n");
    }
    
    /* Damit überhaupt ein HTML-Dokument ausgegeben wird */
    void print_header()
    {
       printf("Content-Type: text/html\n\n");
    }
    
    int main(int argc, char *argv[])
    {
        MYSQL_RES *mTabelle;
        MYSQL_ROW mRecord;
        MYSQL *mConnection, mDB;
        int mError;
        mysql_init(&mDB);
        mConnection = mysql_real_connect(&mDB,"localhost","root","","test",0,0,0);
        print_header();
        print_html_header("Test");
        if (mConnection == NULL)  
        {
            printf("Es konnte keine Verbindung zur Datenbank hergestellt werden: %s \n",mysql_error(&mDB));
            return 1;
        } 
        else 
        {
         printf("Es wurde erfolgreich eine Verbindung hergestellt! \n");
        }
        mysql_close(mConnection);
        print_html_end();
        //system("PAUSE");
        return EXIT_SUCCESS;
    }
    

Anmelden zum Antworten