2012-03-06 53 views
0

我試圖連接到從我的程序MySQL的Web數據庫,但我不斷收到:連接到從我的程序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; 
} 

爲什麼不能連接?

+0

是否將數據庫配置爲允許從外部進行連接,例如:不是本地主機? – 2012-03-06 14:29:39

+0

我不確定這個,你怎麼檢查?你知道任何免費的MySQL免費服務器?該工作 – user1104856 2012-03-06 14:34:44

+0

也許你應該看看你的主機上的網站常見問題:http://www.000webhost.com/faq.php?ID=25 – 2012-03-06 14:37:58

回答

0

如果您使用的是共享虛擬主機,則可能配置mysql服務器拒絕外部連接。