SA-MP Forums Archive
MySQL Connect - 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: MySQL Connect (/showthread.php?tid=655902)



MySQL Connect - KamilPolska - 02.07.2018

I wanted to do the opposite. In "if" we managed to connect and "else" failed connection.
Код:
MySQL_Connect()
{
	new MySQLOpt: option_id = mysql_init_options();

	mysql_set_option(option_id, AUTO_RECONNECT, true);

	g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
	if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
	{
		print("Failed to connect to the database.");
		SendRconCommand("exit");
		return 1;
	}
	print("Connection to the database has been established.");
	
	return 1;
}
Example:
Код:
MySQL_Connect()
{
	new MySQLOpt: option_id = mysql_init_options();

	mysql_set_option(option_id, AUTO_RECONNECT, true);

	g_SQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);
	if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
	{
		// Connect MySQL
	}
	else
	{
		// No connect MySQL
	}
	return 1;
}
Question:
Is this correct?
Код:
if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0)
on
Код:
if(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) == 0)



Re: MySQL Connect - Zeth - 02.07.2018

Well no. Because there is || OR condition and it runs even if any one of the condition is true.
You can try reverting both the conditions but this may not work in all of the conditions. It depends.


Re: MySQL Connect - Calisthenics - 02.07.2018

Does it really matter which one comes first and which one does not?

When you want the opposite, you do exactly that:
pawn Код:
if(g_SQL != MYSQL_INVALID_HANDLE && mysql_errno(g_SQL) == 0)
or simply add the statement inside !(statement)
pawn Код:
if(!(g_SQL == MYSQL_INVALID_HANDLE || mysql_errno(g_SQL) != 0))