Jump to content

[HELP] Get data from 'MySQL'


#Kz

Recommended Posts

Hello dears! Again, Like to always thank you all very much for your helpful answers! I'm very happy to be here with the MTA Community professional script-er and helpers, it's a honor for me!

Today i want to know how i can get a data from the MySQL and put it into a variable, can anyone help me about it?

Example, We've In one of the SQL tables a Phone Number and We wanna get it from MySQL and put the received information to a variable (without using set/getElementData) and use it in a outputChatBox. How we can do that?

 

[Thanks for watching this topic].

 

Link to comment

Haven't done anything like that by myself, but you have to use dbQuery to do this.

Here is example, should be correct but not 100% sure 'cause haven't used mysql or sqlite so far. You can also check wiki for more examples.

-- this inserts data to mydql or sqlite which ever you prefer
dbQuery(dbConnection,"INSERT INTO your_prefered_table_name VALUES (?,?)","first data","second data"); -- you can also add numbers aswell instead of string

-- this will recieve data from mysql or sqlite.
dbQuery(dbConnection,"SELECT * FROM your_prefered_table_name");

 

Edited by Fist
  • Like 1
Link to comment
12 minutes ago, Fist said:

Haven't done anything like that by myself, but you have to use dbQuery to do this.

Here is example, should be correct but not 100% sure 'cause haven't used mysql or sqlite so far. You can also check wiki for more examples.


-- this inserts data to mydql or sqlite which ever you prefer
dbQuery(dbConnection,"INSERT INTO your_prefered_table_name VALUES (?,?)","first data","second data"); -- you can also add numbers aswell instead of string

-- this will recieve data from mysql or sqlite.
dbQuery(dbConnection,"SELECT * FROM your_prefered_table_name");

 

Thanks Man. Helpful!

Link to comment

To actually use the data which was retrieved by dbQuery, you'll have to use dbPoll, which returns a table from the dbQuery's userdata. You'll want to know, if you got any data, because that table can be empty. (There is no data selected from the SQL table, and it's possible if you do a SELECT with WHERE = ?)

local dbConnection = dbConnect(...)
local nrQuery = dbQuery(dbConnection, "SELECT phoneNumber WHERE accID=?", playerData[thePlayer]["accID"]) -- Getting a data from the SQL table with a WHERE.
local rQuery = dbPoll(nrQuery, -1) -- You can set a timeout here, if you are worried about timing out.
if #rQuery > 0 then
	-- Do some work with the phone number.
	local phoneNumber = rQuery[1]["phoneNumber"] -- This would work only, if the player cannot buy more than one phone numbers, if he can, you'll have to cycle through the table. You can only access data got from a dbPoll's table by indexing it with the SQL Table's structure.
end

 

  • Like 2
Link to comment
  • Discord Moderators
On 2017. 04. 17. at 22:05, Fist said:

Haven't done anything like that by myself, but you have to use dbQuery to do this.

Here is example, should be correct but not 100% sure 'cause haven't used mysql or sqlite so far. You can also check wiki for more examples.


-- this inserts data to mydql or sqlite which ever you prefer
dbQuery(dbConnection,"INSERT INTO your_prefered_table_name VALUES (?,?)","first data","second data"); -- you can also add numbers aswell instead of string

-- this will recieve data from mysql or sqlite.
dbQuery(dbConnection,"SELECT * FROM your_prefered_table_name");

 

you should use dbFree too(in case u not need results)

So, you script should look something like this:

function doSomeSQLThing()

local query = dbQuery(dbConnection, " Some SQL command here") 

 -- do something with mysql query if you want.

dbFree(query)

end

 

Link to comment
  • Discord Moderators
1 hour ago, NeXuS™ said:

@Pirulax, there is not so many ways to use a dbQuery, because it returns a query handle, and by dbPolling the query, it automatically gets freed.

I know, that dbPoll frees it.But you need to free a dbQuery(except if u used it in a dbPoll).

https://wiki.multitheftauto.com/wiki/DbQuery

Edited by Pirulax
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...