MySQL Connect
#1

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)
Reply
#2

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.
Reply
#3

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))
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)