SA-MP Forums Archive
/mycars - MYSQL HELP - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: /mycars - MYSQL HELP (/showthread.php?tid=491442)



/mycars - MYSQL HELP - Stefand - 30.01.2014

Hello Community!

I have finally fixed all my MYSQL errors so I can finally continue with my RP script.
I just finished my Ownable Vehicle System but now I got a problem.

When I do /mycars it has to load all the cars from the database that the player owns.
Currently I got this, but it only loads one car. ( the first one it finds)

pawn Код:
command(mycars, playerid, params[])
{
    if(Player[playerid][Adminlevel] == 6)
    {
        new Data[150],Field[258],idx;
        new MyCarPlate[10], MyCarModel, MyCarStoreID;
        mysql_queryS("SELECT * FROM `OwnedVehicles` WHERE `VehicleOwner` = '%s'",GetName(playerid));
        mysql_store_result();
        if(mysql_num_rows() == 1){
            mysql_fetch_rowEx(Data);
            Field = strtuk( Data, idx, '|' );
            for(new s = 0; s < 17; s++)
            {
                switch(s)
                {
                    case 0: MyCarStoreID = strval(Field);
                    case 1: MyCarModel = strval(Field);
                    case 10: strmid(MyCarPlate,Field,0,strlen(Field),10);

                }
                Field = strtuk( Data, idx, '|' );
            }
            fSendClientMessage(playerid, WHITE, "> #%d, ModelID: %d, Plate: %s",MyCarStoreID, MyCarModel, MyCarPlate);
        }
        if(mysql_num_rows() == 0){
            SCM(playerid, YELLOW, "> You do not own any vehicles yet!");
        }
    }
    return 1;
}
How can I make it so it checks all the players cars? (Max is going to be 10 if you will need that anywhere)


Re: /mycars - MYSQL HELP - Shockey HD - 30.01.2014

Quote:
Originally Posted by Stefand
Посмотреть сообщение
Hello Community!

I have finally fixed all my MYSQL errors so I can finally continue with my RP script.
I just finished my Ownable Vehicle System but now I got a problem.

When I do /mycars it has to load all the cars from the database that the player owns.
Currently I got this, but it only loads one car. ( the first one it finds)

pawn Код:
command(mycars, playerid, params[])
{
    if(Player[playerid][Adminlevel] == 6)
    {
        new Data[150],Field[258],idx;
        new MyCarPlate[10], MyCarModel, MyCarStoreID;
        mysql_queryS("SELECT * FROM `OwnedVehicles` WHERE `VehicleOwner` = '%s'",GetName(playerid));
        mysql_store_result();
        if(mysql_num_rows() == 1){
            mysql_fetch_rowEx(Data);
            Field = strtuk( Data, idx, '|' );
            for(new s = 0; s < 17; s++)
            {
                switch(s)
                {
                    case 0: MyCarStoreID = strval(Field);
                    case 1: MyCarModel = strval(Field);
                    case 10: strmid(MyCarPlate,Field,0,strlen(Field),10);

                }
                Field = strtuk( Data, idx, '|' );
            }
            fSendClientMessage(playerid, WHITE, "> #%d, ModelID: %d, Plate: %s",MyCarStoreID, MyCarModel, MyCarPlate);
        }
        if(mysql_num_rows() == 0){
            SCM(playerid, YELLOW, "> You do not own any vehicles yet!");
        }
    }
    return 1;
}
How can I make it so it checks all the players cars? (Max is going to be 10 if you will need that anywhere)
Loops.

I'mma use MAX_VEHICLES for this..

pawn Код:
command(mycars, playerid, params[])
{
    if(Player[playerid][Adminlevel] == 6)
    {
        for(new i=0; i<MAX_VEHICLES; i++)
        {
            new Data[150],Field[258],idx;
            new MyCarPlate[10], MyCarModel, MyCarStoreID;
            mysql_queryS("SELECT * FROM `OwnedVehicles` WHERE `VehicleOwner` = '%s'",GetName(playerid));
            mysql_store_result();
            if(mysql_num_rows() == 1)
            {
                mysql_fetch_rowEx(Data);
                Field = strtuk( Data, idx, '|' );
                for(new s = 0; s < 17; s++)
                {
                    switch(s)
                    {
                        case 0: MyCarStoreID = strval(Field);
                        case 1: MyCarModel = strval(Field);
                        case 10: strmid(MyCarPlate,Field,0,strlen(Field),10);

                    }
                    Field = strtuk( Data, idx, '|' );
                }
                fSendClientMessage(playerid, WHITE, "> #%d, ModelID: %d, Plate: %s",MyCarStoreID, MyCarModel, MyCarPlate);
            }
            if(mysql_num_rows() == 0)
            {
                SCM(playerid, YELLOW, "> You do not own any vehicles yet!");
            }
        }
    }
    return 1;
}



Re: /mycars - MYSQL HELP - Stefand - 30.01.2014

Well that wouldnt work as
1) there are no cars spawned yet
2) there could be unlimited vehicles in that database.

I need something that keeps sending the message untill it finds no other rows anymore.


Re: /mycars - MYSQL HELP - Stefand - 30.01.2014

Код:
A Player buys a car -> It stores it in the `OwnedVehicles` Table.
A Player wants to see all his vehicles -> he types /mycars, which would sum up all the vehicles with his name in the `OwnedVehicles` Table.
A Player wants to spawn one of his cars -> he types /getmycar [id] (1-10), so when they type /mycars the ids need to get assigned somewhere.
That is what I am actually trying to achieve.


Re: /mycars - MYSQL HELP - Shockey HD - 30.01.2014

MAX_VEHICLES is preset, It will loop to the maximum number of vehicles, Although you could also change that.

What I do is I get the total amount of cars in the database, then loop it off of that.

pawn Код:
stock ReturnCCount()
{
    mysql_query("SELECT * FROM OwnedVehicles");

   
    mysql_store_result();
    new users = mysql_num_rows();

    mysql_free_result();
    return users;
}

then

pawn Код:
command(mycars, playerid, params[])
{
    if(Player[playerid][Adminlevel] == 6)
    {
        for(new i=0; i<ReturnCCount(); i++)
        {
            new Data[150],Field[258],idx;
            new MyCarPlate[10], MyCarModel, MyCarStoreID;
            mysql_queryS("SELECT * FROM `OwnedVehicles` WHERE `VehicleOwner` = '%s'",GetName(playerid));
            mysql_store_result();
            if(mysql_num_rows() == 1)
            {
                mysql_fetch_rowEx(Data);
                Field = strtuk( Data, idx, '|' );
                for(new s = 0; s < 17; s++)
                {
                    switch(s)
                    {
                        case 0: MyCarStoreID = strval(Field);
                        case 1: MyCarModel = strval(Field);
                        case 10: strmid(MyCarPlate,Field,0,strlen(Field),10);

                    }
                    Field = strtuk( Data, idx, '|' );
                }
                fSendClientMessage(playerid, WHITE, "> #%d, ModelID: %d, Plate: %s",MyCarStoreID, MyCarModel, MyCarPlate);
            }
            if(mysql_num_rows() == 0)
            {
                SCM(playerid, YELLOW, "> You do not own any vehicles yet!");
            }
        }
    }
    return 1;
}



Re: /mycars - MYSQL HELP - Stefand - 30.01.2014

^ Good idea but it doesnt show anything ingame.


Re: /mycars - MYSQL HELP - PowerPC603 - 30.01.2014

pawn Код:
if(mysql_num_rows() == 1)
It's because you're only processing one car.

It there are more rows returned, your code doesn't do anything, as it skips both if-statements.

You should loop through all rows returned, fetch the data from each row and display it.


Re: /mycars - MYSQL HELP - Stefand - 30.01.2014

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
pawn Код:
if(mysql_num_rows() == 1)
It's because you're only processing one car.

It there are more rows returned, your code doesn't do anything, as it skips both if-statements.

You should loop through all rows returned, fetch the data from each row and display it.
I always used another mysql plugin but a friend told me i should switch to this one I am using now, can you give me a example or do it with my code?


Re: /mycars - MYSQL HELP - PowerPC603 - 30.01.2014

I'm still learning MySQL myself.

I just noticed you used the rowcount to be 1 or 0, so you were unable to load cars when there are more of them.


EDIT:
Just found this:
pawn Код:
//some query
mysql_store_result();
while(mysql_retrieve_row()) //it will be running until there are no more rows to be retrieved
{
   mysql_get_field("fieldname",wheretostorevalue); //macro for mysql_fetch_field_row
   mysql_get_field(...);
   mysql_get_field(...);
   mysql_get_field(...);
   mysql_get_field(...);
}
mysql_free_result();



Re: /mycars - MYSQL HELP - Stefand - 31.01.2014

Fixed