为什么在运行c ++ CGI时会出现不同的控制台和浏览器?

时间:2014-03-27 11:55:13

标签: c++ mysql++

我正在使用cgicc和mysql ++库来开发c ++中的CGI程序。虽然我能够编译程序并构建可执行文件,但是可以在浏览器中看到完整的结果。

Here is the data:
Hello!

select * from table_cities
0

我无法弄清楚为什么cout << res.num_rows();在浏览器输出的情况下返回0而在控制台的情况下返回18(实际值)?

我可以在控制台中看到完整的结果。

可能是什么原因。我错过了代码中的任何内容吗?

#include <iostream>
#include "cgicc/Cgicc.h"
#include "cgicc/HTTPHTMLHeader.h"
#include "cgicc/HTMLClasses.h"
#include <stdlib.h>

// mysql must come after cgi stuff for some reason
#include <mysql++.h>

using namespace cgicc;
using namespace std;
using namespace mysqlpp;

// Helper function for form text boxes and lists
string getFormString(Cgicc& cgi, char *element) 
{
  const_form_iterator name = cgi.getElement(element);
  if(name != (*cgi).end() && ! name->isEmpty())
    return (string)(**name);
  else
    return (string)"";
}

int main(int argc, char* argv[])
{
  Cgicc cgi;
  cout << HTTPHTMLHeader() << endl;

  //cout << HTMLDoctype(HTMLDoctype::eStrict) << endl;
  cout << html() << endl;
  cout << head(title("Results")) << endl;

  cout << body() << endl;
  cout << h1("Here is the data:") << endl;
  cout << "Hello!" << p() << endl;

  try {
  //
    Connection conn(false);
    conn.connect("ts", "127.0.0.1", "root", "Devesh#1");

    string querystr = "select * from table_cities";
    cout << querystr << "<br>" << endl;
    Query query = conn.query();
    query << querystr;

    StoreQueryResult res = query.store();
    Row row;
    StoreQueryResult::iterator iter;

    cout << res.num_rows();
    cout << table().set("border=1") << endl;


    for (iter = res.begin(); iter != res.end(); iter++) 
    {
      row = *iter;
      cout << tr() << endl;
      cout << td((const char *)row[0]) << endl;
      cout << td((const char *)row[1]) << endl;
      //cout << td((const char *)row[2]) << endl;
      cout << tr() << endl;
    }
    cout << table() << endl;

  } 

    catch (BadQuery er)
    {   // handle any connection or                      
         // query errors that may come up 
         cerr << "Error: " << er.what() << endl;
         return -1;
    }

  cout << body() << html() << endl;
  return 0;
}

1 个答案:

答案 0 :(得分:0)

使用 conn.connect(&#34; ts&#34;,&#34; localhost&#34;,&#34; root&#34;,&#34; Devesh#1&#34;); 取代 conn.connect(&#34; ts&#34;,&#34; 127.0.0.1&#34;,&#34; root&#34;,&#34; Devesh# 1&#34;); 解决了这个问题。