我无法使用以下代码连接到MySQL远程服务器:
MYSQL conn;
bool con_to_db()
{
conn = mysql_init(0);
conn =mysql_real_connect(conn, host, user, pass, 0, NULL, 0);
if(conn)
//cout<<"connection to atestat databse successful "<<endl;
return true;
else
// cout<<"connection problem: "<<mysql_error(conn)<<endl;
return false;
}
int main()
{
if(con_to_db())
cout<<"connection to atestat databse successful "<<endl;
else
cout<<"connection problem: "<<mysql_error(conn)<<endl;
}
运行此代码后,控制台中将显示一条消息:
连接失败
答案 0 :(得分:0)
在代码MYSQL conn
的第一行。这是mysql_real_connect的原型:
MYSQL * mysql_real_connect(MYSQL * mysql,
const char * host,
const char * user,
const char * passwd,
const char * db,
unsigned int port,
const char * unix_socket,
unsigned long flags);
conn
应该是一个指针。