SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 02/07/2016, 02:57 PM   #1
Juvanii
Huge Clucker
 
Juvanii's Avatar
 
Join Date: Jun 2014
Location: San Andreas
Posts: 373
Reputation: 21
Default MySQL/R33

Hello guys, i have created a table for houses using mysql, a column called houseid is like:


i wanna know how to get the missing house id (1) to use it in /createhouse command like this:
PHP Code:
CMD:createhouse(playeridparams[])
{
    new 
houseid GetFreeHouseID();
    
// rest of the command ...
    
return 1;
}

stock GetFreeHouseID()
{
    for(new 
i50i++)
    {
        new 
query[128], rowsfields;
        
mysql_format(ConnectionHandlequerysizeof(query),"SELECT * FROM houses WHERE houseid = %i"i);
        
mysql_tquery(ConnectionHandlequery"""");
        
cache_get_data(rowsfieldsConnectionHandle);
        if(!
rows) return i;
    }

__________________
Juvanii is offline   Reply With Quote
Old 02/07/2016, 03:00 PM   #2
Vince
Spam Machine
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 10,067
Reputation: 2660
Default Re: MySQL/R33

The primary key mustn't have any meaning. It is merely there to identify the row in the table. In the house structure (probably the enum) you must create a variable that will hold this id. If you want to create a new house you do a normal insert. Then you use cache_insert_id() to assign the newly created id to said variable.
__________________
Vince is offline   Reply With Quote
Old 02/07/2016, 03:24 PM   #3
Juvanii
Huge Clucker
 
Juvanii's Avatar
 
Join Date: Jun 2014
Location: San Andreas
Posts: 373
Reputation: 21
Default Re: MySQL/R33

can you explain more please ?
__________________
Juvanii is offline   Reply With Quote
Old 02/07/2016, 03:27 PM   #4
SecretBoss
Gangsta
 
SecretBoss's Avatar
 
Join Date: Dec 2014
Location: Greece
Posts: 807
Reputation: 106
Default Re: MySQL/R33

If you have A_I enabled on your INSERT query the house id will be increased automatically then you can retrieve it with a query or save it on a variable with cache_insert_id()
__________________
SecretBoss is offline   Reply With Quote
Old 02/07/2016, 03:50 PM   #5
Konstantinos
Spam Machine
 
Konstantinos's Avatar
 
Join Date: Dec 2011
Posts: 11,828
Reputation: 1401
Default Re: MySQL/R33

The thing is that auto increment is not set to "houseid" column. There are more issues than that with the code itself:
- Executing 50 queries when 1 is enough (looping through the rows returned, there's rowid in cache_get_row(_int/_float) functions).
- Not specifying callback to retrieve the data afterwards.
- Calling cache functions out of the callback will result in warnings for no active cache.

Enable auto increment for "houseid" column and don't have it as variable[index] where index is the "houseid" from the database. Have a separate variable that will hold each "houseid" from the database so you can update according to that the correct row («here»).

As for the command: When first loading the houses, have a global variable that will store the houses loaded. In the command, check if the value of the variable is not equal to the max houses (reached limit) and assign the appropriate values to the array with index the value of the global variable that stores the number of loaded houses. When you do that, execute an INSERT query to insert a new record and specify a callback in which you'll retrieve the "houseid" from the database returned using cache_insert_id function as the above users already mentioned. Store it to the variable I mentioned above (look at «here» because I mentioned more).
__________________
Life is like riding a bicycle. To keep your balance, you must keep moving.

[Tutorial] How to use SQLite
[FilterScript] Tune System
Konstantinos is offline   Reply With Quote
Old 02/07/2016, 04:32 PM   #6
Juvanii
Huge Clucker
 
Juvanii's Avatar
 
Join Date: Jun 2014
Location: San Andreas
Posts: 373
Reputation: 21
Default Re: MySQL/R33

Auto Increment will increase the houseid one by one.
Consider that i have 4 houses (0, 1, 2, 3) and i have deleted houseid 1 for example, when i create a new house using the command, it will assign houseid 4 instead of the missing id (1) .. i need something to get the missing id to assign it as the new houseid.

Konstantinos can you show me a simple code so i can get to the point ?
Thank you
__________________
Juvanii is offline   Reply With Quote
Old 02/07/2016, 04:41 PM   #7
[cS]Owain
Banned
 
Join Date: Dec 2010
Posts: 76
Reputation: 15
Default Re: MySQL/R33

http://www.codediesel.com/mysql/sequ...-mysql/#p26976
[cS]Owain is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Plugin r39-2 Error: Can't connect to local MySQL server through socket Emre__ Scripting Help 10 30/07/2014 08:26 AM
[Tutorial] [VIDEO] MySQL ORM r38: How to pair GM with MySQL & creation of registration/connection system cArnasLT Tutorials 3 19/05/2014 02:08 PM
BlueG MySQL plugin - Failed (plugins/mysql.so: undefined symbol: _Z13stringvprintfPKcPc) linuxthefish Plugin Development 10 13/07/2013 10:56 AM
[Tool/Web/Other] MySQL DB Manager - Easily manage your MySQL databases with this remote application fiki574 Tools and Files 13 22/07/2012 09:41 AM


All times are GMT. The time now is 07:28 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.