PDA

View Full Version : mysql_query - argument type mismatch error


Melktert
11/04/2017, 07:42 PM
error 035: argument type mismatch (argument 1)

Code:

CheckAccountExists(account[])
{
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = '%s'", account);
mysql_query(string); // This line

mysql_store_result();
new value;
value = mysql_num_rows();
mysql_free_result();
return value;
}

DarkSkull
11/04/2017, 07:52 PM
CheckAccountExists(account[])
{
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = %s", account);
mysql_query(string); // This line

mysql_store_result();
new value;
value = mysql_num_rows();
mysql_free_result();
return value;
}

Try it without the single quotes

Melktert
11/04/2017, 07:54 PM
Still the same error. It's something to do with this line...

mysql_query(string);

DarkSkull
11/04/2017, 08:03 PM
Can you show us where you use the CheckAccountExists Function?

DarkSkull
11/04/2017, 08:07 PM
EDIT:
mysql_query has 2 mandatory arguments.
The first : Connection handle
The second : The query

http://wiki.sa-mp.com/wiki/MySQL/R33#mysql_query


mysql = mysql_connect("host", "user", "database", "pass");

mysql_query(mysql, query);

Melktert
11/04/2017, 08:26 PM
So this is my connection handle...

CheckMySQL()
{
mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS);
}

So I changed the code to


CheckAccountExists(account[])
{
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = '%s'", account);
mysql_query(checkmysql, string); // This line

mysql_store_result();
new value;
value = mysql_num_rows();
mysql_free_result();
return value;
}

and i still get the same error...

DarkSkull
11/04/2017, 08:32 PM
Change your connection handle to this
CheckMySQL()
{
return mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS);
}

On the top of the script just below the function add,

new MySql = CheckMySQL();

Then change your Function to this:

CheckAccountExists(account[])
{
new string[128];
format(string, sizeof(string), "SELECT * FROM Users WHERE Name = '%s'", account);
mysql_query(MySql, string); // This line

mysql_store_result();
new value;
value = mysql_num_rows();
mysql_free_result();
return value;
}

Melktert
11/04/2017, 08:50 PM
After a few minutes of struggling...

This causes the Pawn Compiler Library to stop working...

new MySql = CheckMySQL();

Melktert
11/04/2017, 08:54 PM
EDIT: This is probably where the errors come from...

SavePlayer(playerid)
{
if(!PlayerLogged[playerid])
return 0;

UserStats[playerid][Money] = GetPlayerMoney(playerid);


CheckMySQL();

new string[256];
format(string, sizeof(string), "UPDATE Users SET Password='%s',Money='%d' WHERE Name='%s'", UserStats[playerid][Password], UserStats[playerid][Money], UserStats[playerid][Name]);
mysql_query(MySql, string);
return 1;
}

DarkSkull
11/04/2017, 08:54 PM
new MySql;
CheckMySQL()
{
MySql = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_DB, MYSQL_PASS);
}
Try This:

Dayrion
11/04/2017, 10:06 PM
An example:

new
MySQL:MyDB;

ConnectToMYSQLDataBase()
{
new MySQLOpt:option_id = mysql_init_options();

mysql_set_option(option_id, AUTO_RECONNECT, true);

MySQL = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE, option_id);

if(MySQL == MYSQL_INVALID_HANDLE || mysql_errno(MySQL) != 0)
{
print("[SERVER] Connection failed. This server will exit.");
SendRconCommand("exit");
return 1;
}
mysql_log(ALL);
return 1;
}
Also, in your code when you want to send a query you need to use the "MyDB" like
mysql_query(MyDB, string);