PDA

View Full Version : Register mysql problem



suck000
16th April 2017, 08:49
Hello,
I'm trying to make a register system using mysql. I got some problems while creating the account and saving it to mysql.
Here's my code:

registeraccount(username, password)
{
args = [];
args[0] = "register";
maps\mp\gametypes\_mysql::asyncQuery("SELECT createNewAccount('" + maps\mp\gametypes\_mysql::stripstring(username) + "', '" + maps\mp\gametypes\_mysql::stripstring(password) + "') AS `userid`", ::recievelogin, args);
}

recievelogin(rows, args)
{
if(rows.size && isdefined(rows[0]) && isdefined(rows[0]["userid"]) && int(rows[0]["userid"]) != 0)
{
self.pers["userid"] = int(rows[0]["userid"]);
if(isdefined(rows[0]["rank"]))
{
self.pers["admin"] = int(rows[0]["rank"]);
self thread tellmessage(sprintf(self.pers["admname"] + " Account opened ! Rank : ^1(^7%^1)", self.pers["admin"]));
return;
}
else
{
self.pers["admin"] = 0;
self.pers["admname"] = "";
return;

}
if(args[0] == "register")
self thread tellmessage("Account created !");
self thread tellmessage("In case you forgot them, you will find a screehshot Info.jpeg on UdSSR mod folder");
self execclientcommand("screenshotJpeg Info");
return;
}
else
{
if(args[0] == "login")
self thread tellmessage("Wrong Password. Which means username is taken !");
else
self thread tellmessage("Username already exists");
}

}

case "register":
if(args[2].size >= 3 && args[2].size <= 20)
{
if(args[3].size >= 5 && args[3].size <= 25)
{
registeraccount(args[2], args[3]);
return;
}
self thread tellmessage("pw too short/long");
return;
}
self thread tellmessage("user too short/long");

When i try to !register user pass i get this in console:

query: SELECT createNewAccount('user', 'pass') AS `userid`

But nothing is saved to the database.

I think my createNewAccount table settings are wrong.

1311

Any solutions please?
Thanks

Mitch
16th April 2017, 09:54
Your SQL query is wrong. You need a 'INSERT' query.

See the site below for the SQL basics:
https://www.w3schools.com/sql/sql_insert.asp

Full MySQL documentation:
https://dev.mysql.com/doc/refman/5.7/en/insert.html

IzNoGoD
16th April 2017, 10:00
I see you're using a mysql function. Where is the code for it?

I see you're using a mysql function. Did you allow the user to execute it?

I see you're trying to get multiple columns returned from a mysql function. This is not possible.

I see your code is not properly indented, making some if()'s do different things than you think they do.

I see you're using sprintf plus cod2 string concat. This is weird.

suck000
16th April 2017, 10:04
tv later? izno? i already friend @skype w/ u

IzNoGoD
16th April 2017, 11:13
No, go read a mysql 101 guide.

suck000
16th April 2017, 12:50
Dude you wrote to me the query past july by tv. You just told me to do not touch anything and you did the job. I just need the query.

suck000
16th April 2017, 13:17
I can pay you just help me i really need the server ready so soon.

kung foo man
16th April 2017, 14:22
Just provide the missing code/queries and read some quick MySQL introduction. Stuff like userid should be AUTO_INCREMENT e.g.

No need to rush, missing the basics is the first reason why you don't reach higher goals in time.

@Mitch: it's a MySQL function, which needs to be "selected" to call it, here is some example code: https://github.com/kungfooman/cod2_std/blob/master/persistence.gsc#L23

suck000
16th April 2017, 17:27
I tried this query

CREATE TABLE IF NOT EXISTS `createNewAccount` (
`userid` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(32) DEFAULT NULL,
`password` varchar(32) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

nothing as usal.
I do !register user pass, console gets

query: SELECT createNewAccount('test12', 'test12') AS `userid`

And instead of getting message ingame saying account created, i get username already excists.
I try !login test12 test12 i get nothing.
I checked columns of createnewaccount table, nothing in there. It's like the query ain't working at all.

IzNoGoD
16th April 2017, 17:49
I see you're using a mysql function. Where is the code for it?

I see you're using a mysql function. Did you allow the user to execute it?

I see you're trying to get multiple columns returned from a mysql function. This is not possible.

This still applies.

suck000
16th April 2017, 17:54
I granted to my user all rights.
--

my _mysql.gsc file:

init()
{
//get your host, user, pass, db, port here
level.JH_mysql = mysql_reuse_connection();
if(!isDefined(level.JH_mysql))
level.JH_mysql = initMySQL("MYIP", "USER", "PW", "COD", 3306);
initAsyncMySQL("MYIP", "USER", "PW", "COD", 3306);
}

query(query)
{
if(!isDefined(level.JH_mysql))
return undefined;
result = mysql_query(level.JH_mysql, query);
resettimeout();
if(result)
{
printf("Error in " + query + "\n");
return undefined;
}
result = mysql_store_result(level.JH_mysql);
rows = getRows(result);
return rows;
}

queryNosave(query)
{
if(!isDefined(level.JH_mysql))
return undefined;
result = mysql_query(level.JH_mysql, query);
resettimeout();
if(result)
{
printf("Error in " + query + "\n");
return undefined;
}
return [];
}

getRows(result)
{
if(!isDefined(result))
return [];
rowcount = mysql_num_rows(result);
fields = [];
field = mysql_fetch_field(result);
while(isDefined(field))
{
fields[fields.size] = field;
field = mysql_fetch_field(result);
}
rows = [];
for(i = 0; i < rowcount; i++)
{
row = mysql_fetch_row(result);
rows[rows.size] = [];
for(j = 0; j < fields.size; j++)
rows[rows.size - 1][fields[j]] = row[j];
}
mysql_free_result(result);
return rows;
}

asyncQuery(query, function, args)
{
printf("query: %\n", query);
id = mysql_async_create_Query(query);
task = spawnstruct();
task.query = query;
task.invoker = self;
task.function = function;
task.args = args;
level.JH_mysqlAsync["" + id] = task;
}

asyncQueryNosave(query, function, args)
{
printf("querynosave: %\n", query);
id = mysql_async_create_Query_Nosave(query);
task = spawnstruct();
task.query = query;
task.invoker = self;
task.function = function;
task.args = args;
level.JH_mysqlAsync["" + id] = task;
}

initAsyncMySQL(host, user, pass, db, port)
{
mysql_async_initializer(host, user, pass, db, port, 4);
level.JH_mysqlAsync = [];
thread loopAsyncMySQL();
}

loopAsyncMySQL()
{
while(true)
{
list = mysql_async_GetDone_List();
for(i = 0; i < list.size; i++)
{
result = mysql_async_getresult_and_free(list[i]);
if(!isdefined(result))
continue;
if(result == 0)
result = undefined;
task = "" + list[i];
if(isDefined(level.JH_mysqlAsync[task]))
{
if(isDefined(level.JH_mysqlAsync[task].function))
{
if(isDefined(level.JH_mysqlAsync[task].invoker))
{
rows = getRows(result);
level.JH_mysqlAsync[task].invoker thread [[level.JH_mysqlAsync[task].function]](rows, level.JH_mysqlAsync[task].args);
}
else if(isDefined(result))
mysql_free_result(result);
}
else if(isDefined(result))
mysql_free_result(result);
}
else if(isDefined(result))
mysql_free_result(result);
level.JH_mysqlAsync[task] = undefined;
}
wait .05;
}
}

initMySQL(host, user, pass, db, port)
{
mysql = mysql_init();
ret = mysql_real_connect(mysql, host, user, pass, db, port);
if(!ret)
{
printf("errno=" + mysql_errno(mysql) + " error= " + mysql_error(mysql) + "\n");
mysql_close(mysql);
return undefined;
}
return mysql;
}
stripString(string)
{
if(isDefined(level.JH_mysql))
return mysql_real_escape_string(level.JH_mysql, string);
return "";
}

Is that what you're looking for?

kung foo man
16th April 2017, 18:04
Ok, so Mitch was right, you try to insert data into a table via "calling" it. That's obviously wrong, but it's simple to make you learn it:


https://www.youtube.com/watch?v=SwCc4-75cEs

suck000
16th April 2017, 18:30
Iznogod wrote that register script for me last year and it was working.

IzNoGoD
16th April 2017, 19:37
Iznogod wrote that register script for me last year and it was working.

If it was working last year then you fucked something up along the way.

Also, I looked that the code I posted back then, and it shouldn't have worked. Some coding mistakes in it.