Jump to content

MySQL Help - List all things from DB


Recommended Posts

Okay I've got 4 columns, ID, Name, Vehicle and Description.

I want to display all of that from my database using mysql_query, how would I do that?

CLIENT SIDE ------

function showStats()
        mainWn = guiCreateWindow(0.30, 0.15, 0.42, 0.69, "Las Barrancas Garage - View Orders", true)
        guiWindowSetMovable(mainWn, false)
        guiWindowSetSizable(mainWn, false)

        gridList = guiCreateGridList(0.02, 0.05, 0.96, 0.83, true, mainWn)
        guiGridListAddColumn(gridList, "ID", 0.2)
        guiGridListAddColumn(gridList, "Name", 0.2)
        guiGridListAddColumn(gridList, "Vehicle", 0.2)
        guiGridListAddColumn(gridList, "Description", 0.2)
        btnClose = guiCreateButton(0.77, 0.90, 0.21, 0.08, "Close", true, mainWn)    
end

I want to display it onto my gridlist, I'm not sure how to do it, my friend helped me last time but I've forgotten.

 

My MySQL table is called 'stats' - 

columns = id (auto_increment/primary) , name, vehicle, desc

Edited by raysmta
Link to comment

Client

function showStats()
        mainWn = guiCreateWindow(0.30, 0.15, 0.42, 0.69, "Las Barrancas Garage - View Orders", true)
        guiWindowSetMovable(mainWn, false)
        guiWindowSetSizable(mainWn, false)

        gridList = guiCreateGridList(0.02, 0.05, 0.96, 0.83, true, mainWn)
        guiGridListAddColumn(gridList, "ID", 0.2)
        guiGridListAddColumn(gridList, "Name", 0.2)
        guiGridListAddColumn(gridList, "Vehicle", 0.2)
        guiGridListAddColumn(gridList, "Description", 0.2)
        btnClose = guiCreateButton(0.77, 0.90, 0.21, 0.08, "Close", true, mainWn)   
        triggerServerEvent("getListData",getLocalPlayer()) 
end


function setData(tab)
for i=1,#tab do
local row = guiGridListAddRow ( gridList  )
guiGridListSetItemText ( gridList, row, 1, tab[i]['id'] , false, false )
guiGridListSetItemText ( gridList, row, 2, tab[i]['name'] , false, false )
guiGridListSetItemText ( gridList, row, 3, tab[i]['vehicle'] , false, false )
guiGridListSetItemText ( gridList, row, 4, tab[i]['desc'] , false, false )
end
end
addEvent("setListData",true)
addEventHandler("setListData",root,setData)

 

Server

function getData()
local result = {}
local query = dbQuery("select * from stats")
result = dbPoll(query,-1)
dbFree(query)
if #result > 0 then
triggerClientEvent(client,"setListData",client,result)
else
triggerClientEvent(client,"setListData",client,{})
end
end
addEvent("getListData",true)
addEventHandler("getListData",root,getData)

This might fulfill your needs

 

Link to comment

Amazing, thank you so much. One last question, if I make a button e.g.-

delBtn = guiCreateButton( x, y ,z, "Delete", mainWin)

How would I make it so, if the player clicks on one of the grid list item, and press delete, and it removes it from the DB/Gridlist at the same time?

 

Link to comment

I thought you were a pro scripter, rays? Giving hate to all the roleplay communities that launch? Claiming people cannot script or are :~ at scripting? Ironic isn't it?

guiCreateButton on your example is completely wrong. A button does not have a Z coordinate because it's a 2 dimensional element. 

Proper: guiCreateButton(x, y, width, height, "Delete", relative, parent)

When the player clicks the button, it triggers onClientGUIClick. Once clicked, figure out which row is selected on your grid list using guiGridListGetSelected. To delete it from the grid list you can use guiGridListRemoveRow and to delete it from the DB you need to run a delete statement, e.g. "DELETE FROM stats ... etc"

Remember don't use the MySQL module as it's now deprecated, use the native MySQL functions such as dbQuery and dbConnect.

Link to comment

Sorry "Master Scripter" no one is perfect like you. I'm still learning SQL scripting. Plus, I haven't scripted for a very long time, so it's a learning process. Also, giving hate to roleplay communities? I'm sure everyone is entitled to their own opinion. As well as that, this section is for helping not for giving your :~ty hate. It was a question I asked, simple. 

Update: I've figured it out how to delete rows, it's fine. 

Edited by raysmta
Link to comment

Calm down and keep your salt levels at minimum.

I'm stating what I've witnessed and I find it extremely ironic how you hate on communities and claim they're :~, constantly crying, undermining them and then you come asking for help from the same people.

75% of my post was helping you, the other 25% was pointing out how pathetic your actions were and what they've led to.

Grow up kid and accept the truth.

Link to comment

Like I've mentioned before this section is for helping, keep your pathetic, false opinions to yourself and use it somewhere else. And note, I don't hate all communities, I'm enjoying VC-RP's current progress and also enjoy Owl Gaming. And asking help on the MTA Forums is available for anyone. I'm not asking anyone to make my scripts. So I think you should grow, and stop going around placing false facts, hiding behind other names.

Link to comment
2 hours ago, raysmta said:

Amazing, thank you so much. One last question, if I make a button e.g.-

delBtn = guiCreateButton( x, y ,z, "Delete", mainWin)

How would I make it so, if the player clicks on one of the grid list item, and press delete, and it removes it from the DB/Gridlist at the same time?

 

Change the query string.

DELETE FROM stats WHERE some_column=some_value;

Link to comment
2 minutes ago, raysmta said:

Thanks.

 

Np, just remember though like the other guy says, use native functions. Using the module is complete ass. it doesn't take you two seconds to write an MYSQL resource
 

function connection()

return dbConnect(*MAKE YOUR OWN VERIBLES*)

end

Added: function has to be exported to work.

Edited by Unitts
Link to comment

no this will do you need

 

local query = dbQuery(database, "SELECT * FROM players")
local result = dbPoll(query,-1)	
for x, ad in ipairs( result ) do
outputChatBox(result[x]['dataname']) 
end

 

7 minutes ago, raysmta said:

So dbPoll gets all the data?

So if I wanted to get all player names first I'd run the query -

local query = dbQuery(database, "SELECT * FROM accounts)

local result = dbPoll(query,-1)

outputChatBox(result, source)

 

That would display all the data from accounts?

 

local query = dbQuery(database, "SELECT * FROM players")
local result = dbPoll(query,-1)	
for x, ad in ipairs( result ) do
for i, v in ipairs( {'kills','deaths','money'} ) do  
outputChatBox(result[x][v]) 
end
end

this is for multiple

20 minutes ago, raysmta said:

So dbPoll gets all the data?

So if I wanted to get all player names first I'd run the query -

local query = dbQuery(database, "SELECT * FROM accounts)

local result = dbPoll(query,-1)

outputChatBox(result, source)

 

That would display all the data from accounts?

is my code working

?

 

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...