Jump to content

Account system with MySQL


TheCapn

Recommended Posts

Hello guys,

At the player's login, I would like to know if his username is registred in the database. If it's registred, I would like to gather all the information of the account and put it on several variables.

However, I quite impressed by the mySQL in LUA so I have difficulties to make request.

Could you give me an exemple of the system I've just described ?

Regards,

Link to comment

There you go: (Please, note you must add some kind of security checks in order to avoid MySQL injections.).

  
local connection = dbConnect("mysql", "dbname=testdb;host=127.0.0.1", "username", "password") 
if not connection then 
    outputChatBox("Could not connect"); 
end 
-- 
function loginPlayer (player, username, password) 
    local query = dbQuery(connection, "SELECT * FROM users WHERE username = '"..username.."' AND password = '"..password.."'"); 
    local result, num_affected_rows, last_insert_id = dbPoll(query, -1); 
    if num_affected_rows >= 1 then 
        for i,v in pairs(result) do 
            -- MySQL stored account data 
        end 
    else 
        outputChatBox("Wrong username or password"); 
    end 
end 
  

Link to comment

Then you use dbPoll.

For example

local query = dbQuery(handler, "SELECT * FROM users") 
local data = dbPoll(query, -1) 
for k,v in ipairs(data) do 
    outputChatBox(v["column1"]) 
end 

It outputs all values saved in 'column1'.

@OFFTOPIC: Wei

'Diet with russian vodka, lose 3 days in one week !' - Polish is the best one. :)

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