Jump to content

[HELP] MySQL Add Data


Ranous

Recommended Posts

Work of the "if ( not result ) then" line although "createdata" does not work.

-- CONNECTION MYSQL -- 
local dbConnection = mysql_connect( host, username, password, dbName ) 
-- CREATE TABLE -- 
createTable = mysql_query (dbConnection ,"CREATE TABLE IF NOT EXISTS accountdatas(accountName TEXT,playerJoin INT)" ) 
-- ADD ACCOUNT DATA -- 
addEventHandler ("onPlayerLogin",root, 
function(_,acName) 
    local userAccountName = getAccountName ( acName ) 
    local result = mysql_query ( dbConnection, "SELECT accountName FROM accountdatas WHERE accountName = "..userAccountName ) 
    if ( not result ) then 
        local createData = mysql_query ( dbConnection, "INSERT INTO accountdatas(accountName,playerJoin) VALUES ("..userAccountName..",".."0"..")") 
        if ( createData ) then 
            outputChatBox ("#C1C1C1*Your account data was successfully created.",source,255,255,255,true) 
        end 
    else 
        mysql_free_result(result) 
    end 
end 
) 

Link to comment

I would strongly recommend moving towards the in-built db functions.

db = dbConnect(...) 
  
addEventHandler("onPlayerLogin", root, 
    function (_, acc) 
        local userAccountName = getAccountName(acc) 
        local qh = dbQuery(db, "SELECT accountName FROM accountdatas WHERE accountName = ?", accName) 
        local result = dbPoll(qh, -1) 
        if (#result == 0) then -- The query returned no rows 
            local createData = dbExec(db, "INSERT INTO accountdatas (accountName, playerJoin) VALUES (?, ?)", userAccountName, 0) 
                if (createData) then 
                    outputChatBox("#C1C1C1*Your account data was successfully created.", source, 255, 255, 255, true) 
                end 
        end 
    end 
) 

The question marks mean 'substitute something into here'. As we can see after the SQL query (in the same function), there are arguments. These are the arguments we pass into the query itself. It's called preparing a statement.

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