MySQL Saving houses
#1

Hi all,
I've started my house system with MySQL but I have problems, just one house saves in the database, i've put 2 in OnGameModeInit (One line, one house)
Here is the script:
pawn Код:
/*
 * S32_House - Create house with just one line (MySQL)!
 * Copyright©System32
 * This file is provided as is (no warranties)
 */

#if defined _S32_House_included
  #endinput
#endif
#define _S32_House_included
#include <a_samp>
#include <a_mysql>
#include <YSI\y_commands>
#include <YSI\y_hooks>

#define PRESSED(%0)                (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
#define MAX_HOUSE 150

enum hInfo
{
    hOwner[24],
    Float: hEnterX,
    Float: hEnterY,
    Float: hEnterZ,
    Float: hExitX,
    Float: hExitY,
    Float: hExitZ,
    hInterior,
    hPrice,
    hVirtualWorld,
};
new HouseInfo[MAX_HOUSE][hInfo];

//new PlayerHaveHouse[MAX_PLAYERS];
new House = -1, House_Price[MAX_HOUSE] = 0, Buy_House[MAX_HOUSE];
new Label = -1;
new Text3D: LabelMake[sizeof(Label)];
new hstring[128] = -1;
new HQuery[300];
new HouseID;
   
stock CreateDynamicHouse(Float: EnterX, Float: EnterY, Float: EnterZ, Interior, Float: InteriorX, Float: InteriorY, Float: InteriorZ, Price, VirtualWorld)
{
    House += 1;
    House_Price[House] = Price;
    Buy_House[House] = CreatePickup(1239, 2, EnterX, EnterY, EnterZ, -1);
    hstring[126] += 1;
    format(hstring, sizeof(hstring), "For sale!\nPrice: %d\nHouse ID: %d\nType /buyhouse to buy house!", Price, HouseID);
    Label += 1;
    LabelMake[Label] = Create3DTextLabel(hstring, 0x21DD00FF, EnterX, EnterY, EnterZ, 40.0, 0);

    format(HQuery, sizeof(HQuery), "SELECT * FROM `house` WHERE `HouseID` = '%d'", HouseID);
    mysql_query(HQuery);
    mysql_store_result();
    if(mysql_num_rows() == 0)
    {
        HouseInfo[HouseID][hEnterX] = EnterX;
        HouseInfo[HouseID][hEnterY] = EnterY;
        HouseInfo[HouseID][hEnterZ] = EnterZ;
        HouseInfo[HouseID][hExitX] = InteriorX;
        HouseInfo[HouseID][hExitY] = InteriorY;
        HouseInfo[HouseID][hExitZ] = InteriorZ;
        HouseInfo[HouseID][hInterior] = Interior;
        HouseInfo[HouseID][hVirtualWorld] = VirtualWorld;
        HouseInfo[HouseID][hPrice] = Price;
        format(HQuery, sizeof(HQuery), "INSERT INTO `house` (`User`, `EnterX`, `EnterY`, `EnterZ`, `ExitX`, `ExitY`, `ExitZ`, `Interior`, `Price`, `VirtualWorld`, `HouseID`) VALUES");
        format(HQuery, sizeof(HQuery), "%s  ('None', '%f', '%f', '%f', '%f', '%f', '%f', '%d', '%d', '%d', '%d')", HQuery, HouseInfo[HouseID][hEnterX], HouseInfo[HouseID][hEnterY], HouseInfo[HouseID][hEnterZ], HouseInfo[HouseID][hExitX], HouseInfo[HouseID][hExitY], HouseInfo[HouseID][hExitZ], HouseInfo[HouseID][hInterior], HouseInfo[HouseID][hPrice], HouseInfo[HouseID][hVirtualWorld], HouseID);
        mysql_query(HQuery);
        printf("House ID %d created", HouseID);//debug
    }
    else
    {
        if(mysql_fetch_row_format(HQuery, "|"))
        {
            new hsavingstring[1000];
            mysql_fetch_field_row(hsavingstring, "User"); HouseInfo[HouseID][hOwner] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "EnterX"); HouseInfo[HouseID][hEnterX] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "EnterY"); HouseInfo[HouseID][hEnterY] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "EnterZ"); HouseInfo[HouseID][hEnterZ] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "ExitX"); HouseInfo[HouseID][hExitX] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "ExitY"); HouseInfo[HouseID][hExitY] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "ExitZ"); HouseInfo[HouseID][hExitZ] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "Interior"); HouseInfo[HouseID][hInterior] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "Price"); HouseInfo[HouseID][hPrice] = strval(hsavingstring);
            mysql_fetch_field_row(hsavingstring, "VirtualWorld"); HouseInfo[HouseID][hVirtualWorld] = strval(hsavingstring);
        }
    }
    mysql_free_result();
    HouseID ++;
    return 1;
}

Hook:H1_OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if(newkeys & KEY_SECONDARY_ATTACK && !IsPlayerInAnyVehicle(playerid))
    {
        for(new i = 0; i < MAX_HOUSE; i++)
        {
            if(IsPlayerInRangeOfPoint(playerid, 1.5, HouseInfo[i][hEnterX], HouseInfo[i][hEnterY], HouseInfo[i][hEnterZ]) && GetPlayerInterior(playerid) == 0 && GetPlayerVirtualWorld(playerid) == 0)
            {
                SetPlayerInterior(playerid, HouseInfo[i][hInterior]);
                SetPlayerVirtualWorld(playerid, HouseInfo[i][hVirtualWorld]);
                SetPlayerPos(playerid, HouseInfo[i][hExitX], HouseInfo[i][hExitY], HouseInfo[i][hExitZ]);
            }
            else if(IsPlayerInRangeOfPoint(playerid, 1.5, HouseInfo[i][hExitX], HouseInfo[i][hExitY], HouseInfo[i][hExitZ]) && GetPlayerInterior(playerid) == HouseInfo[i][hInterior] && GetPlayerVirtualWorld(playerid) == HouseInfo[i][hVirtualWorld])
            {
                SetPlayerInterior(playerid, 0);
                SetPlayerVirtualWorld(playerid, 0);
                SetPlayerPos(playerid, HouseInfo[i][hEnterX], HouseInfo[i][hEnterY], HouseInfo[i][hEnterZ]);
            }
        }
    }
    return 1;
}
Reply
#2

Have you tried running in debug mode? If you haven't, try that and see if its a problem with mysql or other code. It will save people alot of time when they are trying to help. You will then know if its a problem with mysql or some other code. The debug file should be able to help find out why its not saving. (it has for me several times)

pawn Код:
mysql_debug(1);//OnGameModeInit
Reply
#3

I forgot on mysql debug lol ty
Код:
[11:07:08]  

[11:07:08] ---------------------------

[11:07:08] MySQL Debugging activated (10/20/11)

[11:07:08] ---------------------------

[11:07:08]  

[11:07:08] >> mysql_connect( )

[11:07:08] CMySQLHandler::CMySQLHandler() - constructor called.

[11:07:08] CMySQLHandler::CMySQLHandler() - Connecting to "localhost" | DB: "sa:mp" | Username: "root" ...

[11:07:08] CMySQLHandler::Connect() - Connection was successful.

[11:07:08] CMySQLHandler::Connect() - Auto-Reconnect has been enabled.

[11:07:08] >> mysql_ping( Connection handle: 1 )

[11:07:08] CMySQLHandler::Ping() - Connection is still alive.

[11:07:08] >> mysql_query( Connection handle: 1 )

[11:07:08] CMySQLHandler::Query(SELECT * FROM `house` WHERE `HouseID` = '0') - Successfully executed.

[11:07:08] >> mysql_store_result( Connection handle: 1 )

[11:07:08] CMySQLHandler::StoreResult() - Result was stored.

[11:07:08] >> mysql_num_rows( Connection handle: 1 )

[11:07:08] CMySQLHandler::NumRows() - Returned 0 row(s)

[11:07:08] >> mysql_query( Connection handle: 1 )

[11:07:08] CMySQLHandler::Query(INSERT INTO `house` (`User`, `EnterX`, `EnterY`, `EnterZ`, `ExitX`, `ExitY`, `ExitZ`, `Interior`, `Price`, `VirtualWorld`, `HouseID`) VALUES  ('None', '-2521.331542', '-623.472229', '132.771697', '1527.229980', '-11.574499', '1002.097106', '3', '1000', '0', '0')) - Successfully executed.

[11:07:08] >> mysql_free_result( Connection handle: 1 )

[11:07:08] CMySQLHandler::FreeResult() - Result was successfully free'd.
Reply
#4

There's only one query in the debug file. Are you sure your creating more than one house when you tried that?

Whats this for?

pawn Код:
hstring[126] += 1;
Reply
#5

Your code is very confusing.

You have global variables being used in a stock function, I'm not sure what is going on there.
The name of the function suggests it is creating, or saving a house, but the enclosed query seems to contradict this.

I am not sure what you are putting in gamemodeinit.

Perhaps you should explain in 30 words or more what the function is meant to do, and then I can continue.
Reply
#6

Quote:
Originally Posted by Rachael
Посмотреть сообщение
You have global variables being used in a stock function, I'm not sure what is going on there.
...
Its valid to use global variables in a stock function. I agree with the rest though.
Reply
#7

@iggy1 - string for label, don't worry

@Rachel - So, that code have to create house but if house exists it have to load house, than player can enter it, buy etc...
OnGameModeInit:
pawn Код:
CreateDynamicHouse(-2521.3315,-623.4722,132.7717, 3, 1527.229980,-11.574499,1002.097106, 1000, 0);
    CreateDynamicHouse(-2504.2537,-624.8500,132.7227, 5, 2350.339843,-1181.649902,1027.976562, 15000, 0);
Reply
#8

Have a print at the top of the CreateDynamicHouse stock to check if it's actually being called twice. I looked at that code and their doesn't seem to be any issues with the code itself.
Reply
#9

Quote:
Originally Posted by [HiC]TheKiller
Посмотреть сообщение
Have a print at the top of the CreateDynamicHouse stock to check if it's actually being called twice. I looked at that code and their doesn't seem to be any issues with the code itself.
Function is okay, it calls twice but in database is just first house :S
Reply
#10

Here is something to look at.
https://sampwiki.blast.hk/wiki/MySQL#mysql_fetch_row_format

I think you meant to use this
https://sampwiki.blast.hk/wiki/MySQL#mysql_retrieve_row

and here are a couple more things
pawn Код:
new Text3D: LabelMake[sizeof(Label)]; //what is the size of Label?
new hstring[128] = -1; // ?
hstring[126] += 1; // ?
format(hstring, sizeof(hstring) //...and then format it?

if(mysql_fetch_row_format(HQuery, "|")) // see above
HouseInfo[HouseID][hOwner] = strval(hsavingstring); //need to use format or strmid here
HouseInfo[HouseID][hEnterX] = strval(hsavingstring); //need to use floatstr here
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)