Help to understand code -
,TomY' - 04.08.2018
Hi all. I'm trying to understand code. When my gamemode starts, after OnGameModeInit I have function call:
Код:
mysql_function_query(MysqlConnection,fquery,true,"CarCheck","");
Код:
forward CarCheck();
public CarCheck()
{
new rows,fields;
cache_get_data(rows,fields);
new fetch[24];
new msg[200];
if(rows>0)
{
for(new rowas = 0; rowas != rows; rowas++)
{
cache_get_field_content(rowas, "Name", fetch);
format( msg, sizeof(msg), "SELECT * FROM `users` WHERE Name = '%s'",fetch);
mysql_function_query(MysqlConnection,msg,true,"CarCheck2","s",fetch);
}
}
return 1;
}
Код:
forward CarCheck2(name[]);
public CarCheck2(name[])
{
new rows,fields;
cache_get_data(rows,fields);
new msg[200];
if(rows==0)
{
format( msg,sizeof( msg ),"DELETE FROM `cars` WHERE Name = '%s'",Name);
mysql_function_query(MysqlConnection,msg,false,"","");
return 1;
}
return 1;
}
Can you help me do understand, when server delete the car? I do not understand, what code checking and when he delete car, when not.
Re: Help to understand code -
Sew_Sumi - 04.08.2018
A real good step to reading and understanding, is making your code readable, and consistent.
Код:
forward CarCheck();
public CarCheck()
{
new rows,fields;
cache_get_data(rows,fields);
new fetch[24];
new msg[200];
if(rows>0)
{
for(new rowas = 0; rowas != rows; rowas++)
{
cache_get_field_content(rowas, "Name", fetch);
format( msg, sizeof(msg), "SELECT * FROM `users` WHERE Name = '%s'",fetch);
mysql_function_query(MysqlConnection,msg,true,"CarCheck2","s",fetch);
}
}
return 1;
}
Код:
forward CarCheck2(name[]);
public CarCheck2(name[])
{
new rows,fields;
cache_get_data(rows,fields);
new msg[200];
if(rows==0)
{
format( msg,sizeof( msg ),"DELETE FROM `cars` WHERE Name = '%s'",Name);
mysql_function_query(MysqlConnection,msg,false,"","");
return 1;
}
return 1;
}
Looks a lot better and readable.
Now, if you put in some print statements, you'll get some more info back.
PHP код:
forward CarCheck();
public CarCheck()
{
new rows,fields;
cache_get_data(rows,fields);
new fetch[24];
new msg[200];
if(rows>0)
{
for(new rowas = 0; rowas != rows; rowas++)
{
cache_get_field_content(rowas, "Name", fetch);
format( msg, sizeof(msg), "SELECT * FROM `users` WHERE Name = '%s'",fetch);
printf('CarCheck for %s', fetch);
mysql_function_query(MysqlConnection,msg,true,"CarCheck2","s",fetch);
}
}
return 1;
}
PHP код:
forward CarCheck2(name[]);
public CarCheck2(name[])
{
new rows,fields;
cache_get_data(rows,fields);
new msg[200];
if(rows==0)
{
format( msg,sizeof( msg ),"DELETE FROM `cars` WHERE Name = '%s'",Name); //This won't fire, due to name, not Name....???
printf('CarCheck2 (Deletion) for %s', name);
mysql_function_query(MysqlConnection,msg,false,"","");
return 1;
}
return 1;
}
Mind out for mixing variable methods. UPPERCASE, lowercase, Capitalized and all that. Keep it consistent from the start, and you'll find it easier to pick up where you left off in the future.
Re: Help to understand code -
,TomY' - 04.08.2018
Ok... But I still do not understand, when code delete the car, when not. What is code checking? For example, there are 10 cars created in database. Which will be deleted?
Re: Help to understand code -
Sew_Sumi - 04.08.2018
Check my edit, is what I mentioned at the bottom the real reason you're having trouble?
Is that function (CarCheck2 with regards to Name vs name), not actually working as it's intended, and you're ending up with random cars maybe being deleted?
Re: Help to understand code -
,TomY' - 04.08.2018
Ou.. I see that mistake with Name and name, but it my ,,editions''. In original code that line is with small n:
Код:
format( msg,sizeof( msg ),"DELETE FROM `cars` WHERE Name = '%s'",name);
And server sometimes delete random cars ( not every time, just sometimes and for random players... ). So I try to understand, what is checking this code, and maybe I do not need that code..
Re: Help to understand code -
Sew_Sumi - 04.08.2018
Use printf more, and also use print by itself as it will also show in the logs, so you can simply chuck in a line which will show in the log, that it reached that code, and under what situation.
PHP код:
print('Got to the point I was wondering about');
Re: Help to understand code -
,TomY' - 04.08.2018
I tried to restart server a few times, and all time in log I got:
CarCheck for NULL
CarCheck2 (Deletion) for NULL
As I understand, code does not delete any car now.
Re: Help to understand code -
Sew_Sumi - 04.08.2018
Yes, but it also depends as the DB may have nothing to delete.
Check the CarCheck, and force something to fit the criteria for it to fire, then check that same log entry.