gpt4 book ai didi

c++ - 为什么运行 C++ CGI 时控制台和浏览器的结果不同?

转载 作者:太空宇宙 更新时间:2023-11-04 13:53:08 25 4
gpt4 key购买 nike

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

最佳答案

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

关于c++ - 为什么运行 C++ CGI 时控制台和浏览器的结果不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22686860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com