【问题标题】:Why different result in console and browser while running c++ CGI?为什么在运行 c++ CGI 时控制台和浏览器会产生不同的结果?
【发布时间】:2014-05-06 09:09:54
【问题描述】:

我正在使用 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;
}

【问题讨论】:

    标签: c++ mysql++


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-18
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      相关资源
      最近更新 更多