SA-MP Forums Archive
If mysql connection failed try other connection? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: If mysql connection failed try other connection? (/showthread.php?tid=425411)



If mysql connection failed try other connection? - Admigo - 25.03.2013

Heey all,

Is it possible if the connection is failed to the database to connect to another database?
Example: if the server cant connect to the local database connect to the hosted database.
How can i do this?

Admigo


Re: If mysql connection failed try other connection? - park4bmx - 25.03.2013

yes,
in MYSQL when you cant connect to the server/database it will return a connection handler which is 0 for Blug's.
But yes with that u can check if it's connected or not

soo.
pawn Code:
new mysql = mysql_connect("127.0.0.1","root","mydatabase","mypass");
if(mysql==0) return print("MySQL Connection not established!");



Re: If mysql connection failed try other connection? - reckst4r - 25.03.2013

Try something like this:
pawn Code:
mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS);
if(mysql_ping()>2)
mysql_connect(SQL2_HOST, SQL2_USER, SQL2_DB, SQL2_PASS);



Re: If mysql connection failed try other connection? - InfiniTy. - 25.03.2013

Assuming you use blueg's mysql plugin
pawn Code:
// At the top \\

new gConn;

#define SQL_HOST_LOCAL "hostname"
#define SQL_USER_LOCAL "user"
#define SQL_PASS_LOCAL "password"
#define SQL_DB_LOCAL "database_name"

#define SQL_HOST_HOST "hostname"
#define SQL_USER_HOST "user"
#define SQL_PASS_HOST "password"
#define SQL_DB_HOST "database_name"

// Anywhere you want(Then add mySQL_init(); in OnGameModeInit callback) \\

stock mySQL_init()
{
    gConn = mysql_connect(SQL_HOST_LOCAL, SQL_USER_LOCAL, SQL_DB_LOCAL, SQL_PASS_LOCAL);

    if(mysql_ping() > 0) {
        printf("» Successfully connected to local database [%s].",SQL_DB_LOCAL);
    }
    else {
        print("» Couldn't connect to local database.");
        gConn = mysql_connect(SQL_HOST_HOST, SQL_USER_HOST, SQL_DB_HOST, SQL_PASS_HOST);
        if(mysql_ping() > 0) {
            printf("» Successfully connected to hosted database [%s].",SQL_DB_HOST);
        } else {
            printf("» Couldn't connect to any of the databases.Closing server...");
            SendRconCommand("exit");
        }
    }
    return 1;
}
Should work :d

Edit->Too slow