1

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

Here is the data:
Hello!

select * from table_cities
0

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

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

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

#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;
}
4

1 回答 1

0

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

于 2014-03-27T12:21:53.083 回答