mysql help
#1

hey i got a problem with mysql this simple script is not loading the mysql database
pawn Код:
// This is a comment
// uncomment the line below if you want to write a filterscript
//#define FILTERSCRIPT

#include <a_samp>

#include <a_samp>
//Gstyles include
#include <a_mysql>
//DCMD (Will be used for our register and login commands later in the tutorial
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
//The following information is only for our wamp server, when we are ready to upload it to our server for use you will need to change it
#define SQL_HOST "localhost" //Standard wamp host
#define SQL_USER "root" //Standard wamp user
#define SQL_PASS "" //Standard wamp password (Nothing)
#define SQL_DB "sa-mp" //This is the database we are going to use (I'll show you how to set it up later :) )
#define mysql_fetch_row_2(%1) mysql_fetch_row_format(%1,"|")

main()
{
    print("\n----------------------------------");
    print(" Blank Gamemode by your name here");
    print("----------------------------------\n");
}



public OnGameModeInit()
{
    if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
    {
        print("SQL connection attempt 1 FAILED!");
        if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
        {
            print("SQL connection attempt 2 FAILED!");
            if(!mysql_connect(SQL_HOST, SQL_USER, SQL_DB, SQL_PASS))
            {
                print("SQL connection attempt 3 FAILED!");
                return 1;
            }
        }
    }
    SetGameModeText("Blank Script");
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnGameModeExit()
{
    return 1;
}

public OnPlayerRequestClass(playerid, classid)
{
    SetPlayerPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraPos(playerid, 1958.3783, 1343.1572, 15.3746);
    SetPlayerCameraLookAt(playerid, 1958.3783, 1343.1572, 15.3746);
    return 1;
}

public OnPlayerConnect(playerid)
{
    new Query[200], Pname[24], escpname[24]; //Creates the Query string and the Pname
    GetPlayerName(playerid, Pname, 24); //Gets the players name
    mysql_real_escape_string(Pname, escpname); //escapes the string so you cant MySQL inject
    format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'", escpname); //Selects the line where the playername is the player
    mysql_query(Query); //Querys the string
    mysql_store_result(); //Stores the result
    if(mysql_num_rows() != 0) //If the user is found
    {
        new PIP[56]; //Creates the IP string
        GetPlayerIp(playerid, PIP, 56); //Gets the players IP
        format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `IP` = '%s'", escpname, PIP); //Checks if the players IP is the same, if so it will auto login :)
        mysql_query(Query); //Queries the result
        mysql_store_result(); //Stores the result
        if(mysql_num_rows() != 0) // If the players IP is the same in the DB
        {
            new line[750];
            SetPVarInt(playerid, "Logged", 1); //Sets the logged variable to 1
            SendClientMessage(playerid, 0x009600AA, "Auto Logged in!");
            if(mysql_fetch_row(line)) //Fetches the line
            {
                   new data[3][50]; //The data strings
                   new data2[4]; //The data variables
                   sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
                   SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
                   SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
                   SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
                   SetPlayerScore(playerid, data2[2]); //Sets players score
                   GivePlayerMoney(playerid, data2[3]); //Sets players cash
                   mysql_free_result();
            }
        }
        if(!mysql_num_rows())
        {
            SendClientMessage(playerid, 0x009600AA, "This account is registered, please login"); //User is registered but IP does not match
            ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
        }
    }
    else
    {
        ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
        SendClientMessage(playerid, 0x009600AA, "This account is not registered, please register!"); // User is not registed
    }
    mysql_free_result(); //Frees the result :)
    return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
    if(GetPVarInt(playerid, "Logged") == 1) //Only saves if the player was logged in
    {
        new query[300]; //Variables
        new Pname[24]; //Variables
        GetPlayerName(playerid, Pname, 24); //Gets the player name
        new escpname[24];
        mysql_real_escape_string(Pname, escpname); //escapes the string so you cant MySQL inject
        format(query, sizeof(query), "UPDATE `playerinfo` SET `score` = '%d',`money` = '%d', `kills` = '%d', `deaths` = '%d'  WHERE `user` = '%s'", GetPlayerScore(playerid), GetPlayerMoney(playerid), GetPVarInt(playerid, "Kills"), GetPVarInt(playerid, "Deaths"), escpname); //Saves all the info
        mysql_query(query); //queries
        mysql_free_result(); //Frees the result
    }
    return 1;
}

public OnPlayerSpawn(playerid)
{
    if(GetPVarInt(playerid, "Logged") == 1 && GetPVarInt(playerid, "MoneyGiven") > 0)
    {
        GivePlayerMoney(playerid, GetPVarInt(playerid, "MoneyGiven")); //Cant give cash on OnPlayerConnect
        SetPVarInt(playerid, "MoneyGiven", 0);
    }
    return 1;
}

public OnPlayerDeath(playerid, killerid, reason)
{
    SetPVarInt(playerid, "Deaths", GetPVarInt(playerid, "Deaths") + 1); //Adds a death to the person who died
    if(killerid != INVALID_PLAYER_ID) SetPVarInt(playerid, "Kills", GetPVarInt(playerid, "Kills") + 1); // Adds a kill to the killer unless the player suicided.
    return 1;
}

public OnVehicleSpawn(vehicleid)
{
    return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
    return 1;
}

public OnPlayerText(playerid, text[])
{
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    if (strcmp("/mycommand", cmdtext, true, 10) == 0)
    {
        // Do something here
        return 1;
    }
    return 0;
}

public OnPlayerEnterVehicle(playerid, vehicleid, ispassenger)
{
    return 1;
}

public OnPlayerExitVehicle(playerid, vehicleid)
{
    return 1;
}

public OnPlayerStateChange(playerid, newstate, oldstate)
{
    return 1;
}

public OnPlayerEnterCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveCheckpoint(playerid)
{
    return 1;
}

public OnPlayerEnterRaceCheckpoint(playerid)
{
    return 1;
}

public OnPlayerLeaveRaceCheckpoint(playerid)
{
    return 1;
}

public OnRconCommand(cmd[])
{
    return 1;
}

public OnPlayerRequestSpawn(playerid)
{
    if(GetPVarInt(playerid, "Logged") == 0)
    {
        new Query[200], Pname[24]; //Variables
        GetPlayerName(playerid, Pname, 24); //Gets the player name
        new escpname[24];
        mysql_real_escape_string(Pname, escpname); //escapes the string so you cant MySQL inject
        format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s'" , escpname); //MySQL string
        mysql_query(Query); //Queries
        mysql_store_result(); //Stores
        if(!mysql_num_rows()) ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel"); //If the user is not found it will show the register dialog
        else ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel"); //If the user is not found it will show the login dialog
        return 0; //Player won't spawn
    }
    return 1;
}

public OnObjectMoved(objectid)
{
    return 1;
}

public OnPlayerObjectMoved(playerid, objectid)
{
    return 1;
}

public OnPlayerPickUpPickup(playerid, pickupid)
{
    return 1;
}

public OnVehicleMod(playerid, vehicleid, componentid)
{
    return 1;
}

public OnVehiclePaintjob(playerid, vehicleid, paintjobid)
{
    return 1;
}

public OnVehicleRespray(playerid, vehicleid, color1, color2)
{
    return 1;
}

public OnPlayerSelectedMenuRow(playerid, row)
{
    return 1;
}

public OnPlayerExitedMenu(playerid)
{
    return 1;
}

public OnPlayerInteriorChange(playerid, newinteriorid, oldinteriorid)
{
    return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    return 1;
}

public OnRconLoginAttempt(ip[], password[], success)
{
    return 1;
}

public OnPlayerUpdate(playerid)
{
    return 1;
}

public OnPlayerStreamIn(playerid, forplayerid)
{
    return 1;
}

public OnPlayerStreamOut(playerid, forplayerid)
{
    return 1;
}

public OnVehicleStreamIn(vehicleid, forplayerid)
{
    return 1;
}

public OnVehicleStreamOut(vehicleid, forplayerid)
{
    return 1;
}

public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
    if(dialogid == 14600)
    {
       if(response)
       {
            if(!strlen(inputtext)) //If the player doesn't enter a pass
            {
                ShowPlayerDialog(playerid, 14600, DIALOG_STYLE_INPUT , "Register", "This account is not registered, please register!", "OK", "Cancel");
                SendClientMessage(playerid, 0xF60000AA, "Please enter a password");
            }
            new PIP[50];
            new Query[200], Pname[24];
            GetPlayerName(playerid, Pname, 24); //Players name
            new escpname[24], escpass[100];
               mysql_real_escape_string(inputtext, escpass);
             mysql_real_escape_string(Pname, escpname); //escapes the string so you cant MySQL inject
            GetPlayerIp(playerid, PIP, 50); //Players IP
            format(Query, sizeof(Query), "INSERT INTO `playerinfo` (`user`, `password`, `kills`, `deaths`, `score`, `money`, IP) VALUES ('%s', '%s', 0, 0, 0, 0, '%s')", escpname, escpass, PIP); //Insert string
            mysql_query(Query); //Queries
            GameTextForPlayer(playerid, "~g~Registered", 2000, 3);
            SendClientMessage(playerid, 0x0000D9AA, "Registered and Logged into your account!");
            SetPVarInt(playerid, "Logged", 1);
        }
    }
    if(dialogid == 15000)
    {
       if(response)
       {
           new Query[200], Pname[24];
           GetPlayerName(playerid, Pname, 24);
           new escpname[24];
           mysql_real_escape_string(Pname, escpname);
           format(Query, sizeof(Query), "SELECT * FROM `playerinfo` WHERE `user` = '%s' AND `password` = '%s'", escpname, inputtext);
           mysql_query(Query);
           mysql_store_result();
           if(!mysql_num_rows())
           {
               SendClientMessage(playerid, 0xF60000AA, "Invalid password!");
               SetPVarInt(playerid, "WrongPass", GetPVarInt(playerid, "WrongPass") + 1);
               ShowPlayerDialog(playerid, 15000, DIALOG_STYLE_INPUT , "Login", "This account is registered, please login", "OK", "Cancel");
               if(GetPVarInt(playerid, "WrongPass") == 3)
               {
                   SendClientMessage(playerid, 0xF60000AA, "Max password tries exceeded!!");
                   Kick(playerid);
               }
           }
           else
           {
               new line[750]; //I know it's big D=
               if(mysql_fetch_row(line)) //Fetches the line
               {
                   new data[3][50]; //The data strings
                   new data2[4]; //The data variables
                   sscanf(line, "p|ssdddds", data[0], data[1], data2[0], data2[1], data2[2], data2[3], data[2]); //Splits the line with sscanf
                   SetPVarInt(playerid, "Kills", data2[0]); //Sets Pvar ints
                   SetPVarInt(playerid, "Logged", 1); //Sets Pvar ints
                   SetPVarInt(playerid, "Deaths", data2[1]); //Sets Pvar ints
                   SetPlayerScore(playerid, data2[2]); //Sets players score
                   SetPVarInt(playerid, "MoneyGiven", data2[3]); //Sets players cash
                   SendClientMessage(playerid, 0x0000D9AA, "Logged in!");
                   mysql_free_result();
               }
           }
       }
    }
    return 1;
}

public OnPlayerClickPlayer(playerid, clickedplayerid, source)
{
    return 1;
}

stock sscanf(string[], format[], {Float,_}:...)
{
    #if defined isnull
        if (isnull(string))
    #else
        if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
    #endif
        {
            return format[0];
        }
    #pragma tabsize 4
    new
        formatPos = 0,
        stringPos = 0,
        paramPos = 2,
        paramCount = numargs(),
        delim = ' ';
    while (string[stringPos] && string[stringPos] <= ' ')
    {
        stringPos++;
    }
    while (paramPos < paramCount && string[stringPos])
    {
        switch (format[formatPos++])
        {
            case '\0':
            {
                return 0;
            }
            case 'i', 'd':
            {
                new
                    neg = 1,
                    num = 0,
                    ch = string[stringPos];
                if (ch == '-')
                {
                    neg = -1;
                    ch = string[++stringPos];
                }
                do
                {
                    stringPos++;
                    if ('0' <= ch <= '9')
                    {
                        num = (num * 10) + (ch - '0');
                    }
                    else
                    {
                        return -1;
                    }
                }
                while ((ch = string[stringPos]) > ' ' && ch != delim);
                setarg(paramPos, 0, num * neg);
            }
            case 'h', 'x':
            {
                new
                    num = 0,
                    ch = string[stringPos];
                do
                {
                    stringPos++;
                    switch (ch)
                    {
                        case 'x', 'X':
                        {
                            num = 0;
                            continue;
                        }
                        case '0' .. '9':
                        {
                            num = (num << 4) | (ch - '0');
                        }
                        case 'a' .. 'f':
                        {
                            num = (num << 4) | (ch - ('a' - 10));
                        }
                        case 'A' .. 'F':
                        {
                            num = (num << 4) | (ch - ('A' - 10));
                        }
                        default:
                        {
                            return -1;
                        }
                    }
                }
                while ((ch = string[stringPos]) > ' ' && ch != delim);
                setarg(paramPos, 0, num);
            }
            case 'c':
            {
                setarg(paramPos, 0, string[stringPos++]);
            }
            case 'f':
            {

                new changestr[16], changepos = 0, strpos = stringPos;
                while(changepos < 16 && string[strpos] && string[strpos] != delim)
                {
                    changestr[changepos++] = string[strpos++];
                    }
                changestr[changepos] = '\0';
                setarg(paramPos,0,_:floatstr(changestr));
            }
            case 'p':
            {
                delim = format[formatPos++];
                continue;
            }
            case '\'':
            {
                new
                    end = formatPos - 1,
                    ch;
                while ((ch = format[++end]) && ch != '\'') {}
                if (!ch)
                {
                    return -1;
                }
                format[end] = '\0';
                if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
                {
                    if (format[end + 1])
                    {
                        return -1;
                    }
                    return 0;
                }
                format[end] = '\'';
                stringPos = ch + (end - formatPos);
                formatPos = end + 1;
            }
            case 'u':
            {
                new
                    end = stringPos - 1,
                    id = 0,
                    bool:num = true,
                    ch;
                while ((ch = string[++end]) && ch != delim)
                {
                    if (num)
                    {
                        if ('0' <= ch <= '9')
                        {
                            id = (id * 10) + (ch - '0');
                        }
                        else
                        {
                            num = false;
                        }
                    }
                }
                if (num && IsPlayerConnected(id))
                {
                    setarg(paramPos, 0, id);
                }
                else
                {
                    #if !defined foreach
                        #define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
                        #define __SSCANF_FOREACH__
                    #endif
                    string[end] = '\0';
                    num = false;
                    new
                        name[MAX_PLAYER_NAME];
                    id = end - stringPos;
                    foreach (Player, playerid)
                    {
                        GetPlayerName(playerid, name, sizeof (name));
                        if (!strcmp(name, string[stringPos], true, id))
                        {
                            setarg(paramPos, 0, playerid);
                            num = true;
                            break;
                        }
                    }
                    if (!num)
                    {
                        setarg(paramPos, 0, INVALID_PLAYER_ID);
                    }
                    string[end] = ch;
                    #if defined __SSCANF_FOREACH__
                        #undef foreach
                        #undef __SSCANF_FOREACH__
                    #endif
                }
                stringPos = end;
            }
            case 's', 'z':
            {
                new
                    i = 0,
                    ch;
                if (format[formatPos])
                {
                    while ((ch = string[stringPos++]) && ch != delim)
                    {
                        setarg(paramPos, i++, ch);
                    }
                    if (!i)
                    {
                        return -1;
                    }
                }
                else
                {
                    while ((ch = string[stringPos++]))
                    {
                        setarg(paramPos, i++, ch);
                    }
                }
                stringPos--;
                setarg(paramPos, i, '\0');
            }
            default:
            {
                continue;
            }
        }
        while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
        {
            stringPos++;
        }
        while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
        {
            stringPos++;
        }
        paramPos++;
    }
    do
    {
        if ((delim = format[formatPos++]) > ' ')
        {
            if (delim == '\'')
            {
                while ((delim = format[formatPos++]) && delim != '\'') {}
            }
            else if (delim != 'z')
            {
                return delim;
            }
        }
    }
    while (delim > ' ');
    return 0;
}
I created the database with phpmyadmin and now it is not loading the server starts running no connection succed or fail here is my log
Код:
[22:55:44]   Loaded.
[22:55:44]  Loading plugin: mysql
[22:55:44] 

  > MySQL plugin R5 successfully loaded.

[22:55:44]   Loaded.
[22:55:44]  Loaded 2 plugins.

[22:55:44] 
[22:55:44] Filter Scripts
[22:55:44] ---------------
[22:55:44]   Loaded 0 filter scripts.

[22:55:44] 
----------------------------------
[22:55:44]  Blank Gamemode by your name here
[22:55:44] ----------------------------------

[22:55:44] Number of vehicle models: 0
I will wait for help cant figure why is not loading
Reply
#2

At the bottom of the OnGameModeInit put "mysql_ping()". Also put a print and even see if the code is reaching that area. If it's not then make sure you have setup the plugin correctly with Libmysql in the server directory etc.
Reply
#3

I tryed to solve the problem with this two steps buts stills not working if someone can take a look at the server and tell me what i lost here is the link http://solidfiles.com/d/d254/ Thank you
Reply
#4

Quote:
Originally Posted by NoobScripter1
Посмотреть сообщение
I tryed to solve the problem with this two steps buts stills not working if someone can take a look at the server and tell me what i lost here is the link http://solidfiles.com/d/d254/ Thank you
You should use StrickenKid's (no offense BlueG). Stricken's is easier to use, and by default it will tell you if you are connected or not. All you need to do is connect, which is mysql_connect(...) //Look at topic.
Reply
#5

You mean this http://forum.sa-mp.com/showthread.ph...t=Stricken%27s ?
Reply
#6

Quote:
Originally Posted by NoobScripter1
Посмотреть сообщение
Yes that's correct.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)