Jump to content

Making dxDrawText show a string from a MySQL database?


Sabre

Recommended Posts

So, let's say I have a query in a server-side script.

mysql:query_free("SELECT 'column' FROM table WHERE id = '" .. mysql:escape_string(uniqueid) .. "'") 

And then, I have a dxDrawText line, on a server-side script:

dxDrawText("??", tx+150, ty+10, tx+size-500, ty, tocolor(0, 255, 0, 200), 0.7, "bankgothic", "center", "center") 

I'd like to know how to display the string from the MySQL column, in the dxDrawText string. Can't seem how to figure it out, since one is client side, and one is server side.

Anyone know?

Link to comment

You can use triggers for the purpose. I made a small example script and I hope it works, can't see anything why it wouldn't. Try it out and change the MySQL query a little so it meets your tables.

The script will make a command handler where you are supposed to enter a unique id in-game. For example /testsql 2. After that the script will send the information to the client-side code, which triggers the dxDrawText function. It should display the data we fetched.

  • /testsql [unique id]

Server-side

addCommandHandler("testsql", 
    function(player, cmd, uniqueid) 
        local uniqueid = tonumber(uniqueid) 
        if uniqueid then 
            local query = mysql:query_free("SELECT username FROM users WHERE id = '" .. mysql:escape_string(uniqueid) .. "'") 
            if query then 
                triggerClientEvent(player, "onDataSelect", player, query.username) 
            else 
                outputChatBox("MySQL error.", player, 255, 0, 0, false) 
            end 
        else 
            outputChatBox("Syntax: /" .. cmd .. " [unique id]", player, 255, 180, 0, false) 
        end 
    end 
) 

Client-side

addEvent("onDataSelect", true) 
addEventHandler("onDataSelect", root, 
    function(username) 
        _username = username 
        addEventHandler("onClientRender", root, showUsername) 
         
        if isTimer(datatimer) then killTimer(datatimer) end 
        datatimer = setTimer(function() 
            removeEventHandler("onClientRender", root, showUsername) 
        end, 6500, 1) 
    end 
) 
  
function showUsername() 
    dxDrawText("Fetched username: " .. _username, tx+150, ty+10, tx+size-500, ty, tocolor(0, 255, 0, 200), 0.7, "bankgothic", "center", "center", false, true, true, false, true) 
end 

Link to comment

Thanks for responding, myonlake, but I'm having trouble with the query, and this is the error I get in the console.

Quote
[2012-11-16 11:02:29] ERROR: clientserverbridge\s_test.Lua:24: attempt to index local 'query' (a number value)

Here's the whole function from s_test.Lua, so you can see maybe if I screwed anything up?

addCommandHandler("testsql", 
   function(player, cmd, uniqueid) 
            local uniqueid = tonumber(uniqueid) 
            if uniqueid then 
                local query = mysql:query("SELECT username FROM accounts WHERE id = '" .. (uniqueid) .. "'") 
                if query then 
                    triggerClientEvent(player, "onDataSelect", player, query.username) 
                else 
                    outputChatBox("MySQL error.", player, 255, 0, 0, false) 
                end 
            else 
                outputChatBox("Syntax: /" .. cmd .. " [unique id]", player, 255, 180, 0, false) 
            end 
        end 
    ) 
 

And then here's the whole function from c_test.Lua, didn't really change anything here, but still.

    addEvent("onDataSelect", true) 
    addEventHandler("onDataSelect", root, 
        function(username) 
            _username = username 
            addEventHandler("onClientRender", root, showUsername) 
            
            if isTimer(datatimer) then killTimer(datatimer) end 
            datatimer = setTimer(function() 
                removeEventHandler("onClientRender", root, showUsername) 
            end, 6500, 1) 
        end 
    ) 
      
    function showUsername() 
        dxDrawText("Fetched username: " .. _username, tx+150, ty+10, tx+size-500, ty, tocolor(0, 255, 0, 200), 0.7, "bankgothic", "center", "center", false, true, true, false, true) 
    end 
 

And here's a screenshot of the accounts table, the ID and the username are right next to each other, all of the mysql is properly configured and all that.

1qOuL.jpg

When I typed /testsql 1 in-game, it didn't display anything in-game, not even an error, just the error I posted in the console.

I would really appreciate it if you could point me in the right direction, or see if I'm messing up the query somehow?

Thanks!

Link to comment

Hmm, perhaps this? I haven't really worked around on separate MySQL resources so I don't know how they return values, sorry.

addCommandHandler("testsql", 
   function(player, cmd, uniqueid) 
            local uniqueid = tonumber(uniqueid) 
            if uniqueid then 
                if mysql:query("SELECT `username` FROM `accounts` WHERE id = '" .. uniqueid .. "'") then 
                    triggerClientEvent(player, "onDataSelect", player, query.username) 
                else 
                    outputChatBox("MySQL error.", player, 255, 0, 0, false) 
                end 
            else 
                outputChatBox("Syntax: /" .. cmd .. " [unique id]", player, 255, 180, 0, false) 
            end 
        end 
    ) 

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