Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从我的程序连接到mysql web数据库_C++_Mysql_C_Xcode_Host - Fatal编程技术网

C++ 从我的程序连接到mysql web数据库

C++ 从我的程序连接到mysql web数据库,c++,mysql,c,xcode,host,C++,Mysql,C,Xcode,Host,我正在尝试从我的程序连接到mysql web数据库,但我一直得到: Can't connect to MySQL server on 'mysql7.000webhost.com' (51) 我正在使用这个网站免费的mysql服务器: 000webhost.com 这是我的代码或我在谷歌中找到的开始学习的代码 #include <mysql.h> #include <stdio.h> #include <stdlib.h> // just going t

我正在尝试从我的程序连接到mysql web数据库,但我一直得到:

Can't connect to MySQL server on 'mysql7.000webhost.com' (51) 
我正在使用这个网站免费的mysql服务器:

000webhost.com

这是我的代码或我在谷歌中找到的开始学习的代码

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

// just going to input the general details and not the port numbers
struct connection_details
{
    char *server;
    char *user;
    char *password;
    char *database;
};

MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
    // first of all create a mysql instance and initialize the variables within
    MYSQL *connection = mysql_init(NULL);

    // connect to the database with the details attached.
    if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, mysql_details.database, 0, NULL, 0)) {
        printf("Conection error : %s\n", mysql_error(connection));
        exit(1);
    }
    return connection;
}

MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
    // send the query to the database
    if (mysql_query(connection, sql_query))
    {
        printf("MySQL query error : %s\n", mysql_error(connection));
        exit(1);
    }

    return mysql_use_result(connection);
}

int main()
{
    MYSQL *conn;        // the connection
    MYSQL_RES *res; // the results
    MYSQL_ROW row;  // the results row (line by line)

    struct connection_details mysqlD;
    mysqlD.server = (char*)"mysql7.000webhost.com";  // where the mysql database is
    mysqlD.user =  (char*)"a1206305_test";      // the root user of mysql   
    mysqlD.password =  (char*)"testthis1"; // the password of the root user in mysql
    mysqlD.database =  (char*)"a1206305_test";  // the databse to pick

    // connect to the mysql database
    conn = mysql_connection_setup(mysqlD);

    // assign the results return to the MYSQL_RES pointer
    res = mysql_perform_query(conn, (char*) "show tables");

    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) !=NULL)
        printf("%s\n", row[0]);

    /* clean up the database result set */
    mysql_free_result(res);
    /* clean up the database link */
    mysql_close(conn);

    return 0;
}
#包括
#包括
#包括
//只需要输入一般细节,而不是端口号
结构连接详细信息
{
字符*服务器;
字符*用户;
字符*密码;
字符*数据库;
};
MYSQL*MYSQL\u连接\u设置(结构连接\u详细信息MYSQL\u详细信息)
{
//首先,创建一个mysql实例并初始化其中的变量
MYSQL*connection=MYSQL_init(NULL);
//连接到已附加详细信息的数据库。
if(!mysql\u real\u connect(connection,mysql\u details.server,mysql\u details.user,mysql\u details.password,mysql\u details.database,0,NULL,0)){
printf(“连接错误:%s\n”,mysql_错误(连接));
出口(1);
}
回路连接;
}
MYSQL\u RES*MYSQL\u执行\u查询(MYSQL*连接,char*sql\u查询)
{
//将查询发送到数据库
if(mysql\u查询(连接,sql\u查询))
{
printf(“MySQL查询错误:%s\n”,MySQL_错误(连接));
出口(1);
}
返回mysql\u使用结果(连接);
}
int main()
{
MYSQL*conn;//连接
MYSQL_RES*RES;//结果
MYSQL_ROW ROW;//结果行(逐行)
结构连接详细信息mysqlD;
mysqlD.server=(char*)“mysql7.000webhost.com”;//mysql数据库所在的位置
mysqlD.user=(char*)“a1206305_test”;//mysql的根用户
mysqlD.password=(char*)“testthis1”;//mysql中根用户的密码
mysqlD.database=(char*)“a1206305_test”;//要拾取的数据库
//连接到mysql数据库
conn=mysql\u连接\u设置(mysqlD);
//将结果返回分配给MYSQL_RES指针
res=mysql_执行_查询(conn,(char*)“show tables”);
printf(“MySQL数据库中的MySQL表:\n”);
while((row=mysql\u fetch\u row(res))!=NULL)
printf(“%s\n”,第[0]行);
/*清理数据库结果集*/
mysql_free_结果(res);
/*清理数据库链接*/
关闭(康涅狄格州);
返回0;
}

为什么不连接?

如果您使用的是共享web主机,mysql服务器可能配置为拒绝外部连接。

数据库是否配置为允许外部连接,例如不允许本地主机?我不确定,您如何检查?你知道有免费的mysql服务器吗?也许你应该看看你的主人网站上的常见问题:上面说:出于安全和服务器性能的原因,远程MySQL连接被禁用。但是,如果升级帐户,将启用远程MySQL连接。但这就是它没有连接的原因吗?如果你没有在mysql服务器的同一台机器上运行你的应用程序(我对此表示怀疑),那么很可能就是这样。