GetBizEmptyID (from ini system to mysql)
#1

PHP код:
stock GetBizEmptyID(const len)
{
    new 
id = (-1);
    for(new 
loop = (0), check= (-1), Data_[64] = "\0"loop != len; ++ loop)
    {
       
check= (loop+1);
       
format(Data_, (sizeof Data_), "BIZS/Biz_%d.ini"check);
       if(!
fexist(Data_))
       {
          
id = (check);
          break;
       }
       }
      return (
id);

How can I convert this to mysql system?

I'm using this function on commande makebusiness:

new bizid = GetFirmaEmptyID(MAX_BIZ);

And than I use that bizid to make new business:

BusinessInfo[bizd][bInfo] = ...;


I have in BusinessInfo enum main SQLID that I can use for businesses ID but don't know how to make it work in mysql. Just transfered my gamemode from ini to mysql and still learning.
Reply
#2

Why would you do that ?
When using mysql you can simply make your id structure auto increment and when you create a biz you can use for your example

new bizid = cache_insert_id();

which will return a last id inserted
Reply
#3

Quote:
Originally Posted by Cadilab
Посмотреть сообщение
Why would you do that ?
When using mysql you can simply make your id structure auto increment and when you create a biz you can use for your example

new bizid = cache_insert_id();

which will return a last id inserted
That is what I needed..

Is cache_insert_id(); for R40? How actually it works? I have SQLID in business enum and I set in my businesses table AI that var..
Reply
#4

I managed to do this with getting ID.

In this command I'm first inserting one free row in table with default values than I'm updating the table with info I want...

So, problem is that when I type /makebusiness, it inserts custom values, and when I type second time same command, it adds / inserts another row with custom values and edits the last one row (from first time I typed command).


First querry for inserting:

PHP код:
new query[400];
    
//Total_Biz_Created++;
    
mysql_format(Databasequerysizeof(query), "INSERT INTO `businesses` (Name, Price, ExteriorX, ExteriorY, ExteriorZ, ExteriorA, InteriorX, InteriorY, InteriorZ, InteriorA, Interior, VirtualWorld, Type) \
        VALUES('BusinessRandomName', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)"
);
    
mysql_tquery(Databasequery"GetBizID""i"playerid); 
Than I put some values I want to update later...:

PHP код:
BusinessInfo[NewBiz[playerid]][Interior] = 2;
    
strins(BusinessInfo[NewBiz[playerid]][Name], "24/7"05);
    
BusinessInfo[NewBiz[playerid]][VirtualWorld] = 5;
    
BusinessInfo[NewBiz[playerid]][Type] = 1;
    
BusinessInfo[NewBiz[playerid]][Owned] = 1
And update the table:

PHP код:
mysql_format(Databasequerysizeof(query), "UPDATE `businesses` SET `Interior` = %d, `Name` = '%e', `VirtualWorld` = %d, `Type` = %d, `Owned` = %d WHERE `SQLID` = %d LIMIT 1",
        
BusinessInfo[NewBiz[playerid]][Interior], BusinessInfo[NewBiz[playerid]][Name], BusinessInfo[NewBiz[playerid]][VirtualWorld], BusinessInfo[NewBiz[playerid]][Type], BusinessInfo[NewBiz[playerid]][Owned], NewBiz[playerid]);
    
mysql_tquery(Databasequery); 
It also won't update Name, it leaves it empty..
Reply
#5

Someone?
Reply
#6

Bump...
Reply
#7

GetBizID is where you should be assigning the ID. Can you run the following query for me?

pawn Код:
EXPLAIN `businesses`
Reply
#8

Quote:
Originally Posted by DobbysGamertag
Посмотреть сообщение
GetBizID is where you should be assigning the ID. Can you run the following query for me?

pawn Код:
EXPLAIN `businesses`
This okay?

Reply
#9

Besides a few possibly needing their type changing from int to tinyint it's fine. Can you show all the code from creating and loading a buisiness (including the part where you assign it an ID)
Reply
#10

Quote:
Originally Posted by DobbysGamertag
Посмотреть сообщение
Besides a few possibly needing their type changing from int to tinyint it's fine. Can you show all the code from creating and loading a buisiness (including the part where you assign it an ID)
https://pastebin.com/hyeHcqea
Reply
#11

Before i reply with an updated pastebin link, can you update the fields you're writing initially as 0 in the /makebusiness command to default value 0? Saves you an extra query.

Edit:

Looking at the code now, you can create either a new ID for it at runtime, by iterating all businesses, and getting a free ID that way, or this way:

pawn Код:
//Your code to create / SQL etc
    mysql_tquery(Database, query, "OnBusinessCreated", "i", playerid);


//You may of needed to thread the query for cache_insert_id(); to work (i only use threaded so i'm unsure if mysql_query(); supports it.

forward OnBusinessCreated(playerid);
public OnBusinessCreated(playerid)
{
    new string[141];
    format(string, sizeof(string), "Business %d created successfully", cache_insert_id());
    SendClientMessage(playerid, COLOR_WHITE, string);
    //You'd unload and load businesses here.
    return 1;
}
It might also be worth setting default values (like i mentioned above), to save lines of code, and query length. You might also want to reload the businesses when they're created, or load the last one at least.

pawn Код:
SELECT * FROM `businesses` ORDER BY `id` DESC LIMIT 1
That should load the last created ID.
Reply
#12

Could you please added your edited pastebin code?

I'm still thinking about whole business system so will try to reduce number of queries and make it optimized as much as I can...

Thanks for replying and helping!
Reply
#13

You don't require an SQL statement. Load the SQL IDs in an array (which you should already be doing), loop through the array and get an unused ID.
Reply
#14

Quote:
Originally Posted by CheezIt
Посмотреть сообщение
You don't require an SQL statement. Load the SQL IDs in an array (which you should already be doing), loop through the array and get an unused ID.
"loop through the array and get an unused ID"

how to?


edit:

I did some testing, in mysql table on phpmyadmin first business ID is 1 but in game every time i create business from last server restart I first create business ID 0 which dont exist in mysql phpmyadmin
Reply
#15

Let me take a different approach since you're not doing things right, or understand how you should use the businesses' SQL IDs and array IDs (ignore what I said previously since you should edit your code to do things properly - read below).

The SQL ID of a business doesn't need to match its array ID, the array ID is just to locate the business' stored information within the script in runtime or however you want to see it, the SQL ID is the identifier of the business within your database. The SQL ID should only be used to UPDATE, INSERT INTO or SELECT from your database.

The SQL IDs don't need to be consecutive (1, 2, 3, 4, 5), reordering them should not be a concern.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)