Sign in to follow this  
Lloyd Logan

mySQL(UNSOLVED)

Recommended Posts

Why does this keep INSERTING even if there is already the same serial in the table?

local data = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serial), -1) 
    if data and type (data) == "table" and #data > 0 then 
    dbExec (server, "UPDATE accounts SET serial = ?, WHERE serial = '"..serial.."'", tostring (serialz)) 
    else 
    dbExec (server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) 
    end 

Share this post


Link to post

Try This:

local data = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serial ) 
if #data > 0 then 
    dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) 
else 
    dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) 
end 

Share this post


Link to post
Try This:
local data = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serial ) 
if #data > 0 then 
    dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) 
else 
    dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) 
end 

It gives me the error; attempt to get length of local 'data' a userdata value

Share this post


Link to post
dbPoll is required to get the result of dbQuery.

So;

function getSerial() 
    local serialz = getPlayerSerial(source) 
    local queryResult = dbQuery ( server, "SELECT serial FROM accounts WHERE serial = ?", serialz ) 
    local database = dbPoll(queryResult, -1) 
    if #database > 0 then 
    dbExec ( server, "UPDATE accounts SET serial = ? WHERE serial = ?", serial, tostring (serialz)) 
    else 
    dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", tostring (serialz)) 
    end 
end 
addEventHandler("onPlayerJoin", getRootElement(), getSerial) 

Share this post


Link to post

Why you do update the serial if it already the same in the database?

You should only insert it if it doesn't exists.

function getSerial() 
    local serialz = getPlayerSerial(source) 
    local database = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serialz), -1) 
    if database and type(database) == "table" and #database == 0 then 
        dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", serialz) 
    end 
end 
addEventHandler("onPlayerJoin", root, getSerial) 

There also another way, but i am not sure if it right or not, you can try it.

function getSerial() 
    local serialz = getPlayerSerial(source) 
    dbExec(server, "INSERT INTO accounts (serial) VALUES (serialz) WHERE NOT EXISTS (SELECT serial FROM accounts WHERE serial = '"..serialz.."'") 
end 
addEventHandler("onPlayerJoin", root, getSerial) 

Share this post


Link to post
Why you do update the serial if it already the same in the database?

You should only insert it if it doesn't exists.

function getSerial() 
    local serialz = getPlayerSerial(source) 
    local database = dbPoll(dbQuery(server, "SELECT serial FROM accounts WHERE serial = ?", serialz), -1) 
    if database and type(database) == "table" and #database == 0 then 
        dbExec ( server, "INSERT INTO accounts (serial) VALUES (?)", serialz) 
    end 
end 
addEventHandler("onPlayerJoin", root, getSerial) 

There also another way, but i am not sure if it right or not, you can try it.

The first on works! Thank you!

function getSerial() 
    local serialz = getPlayerSerial(source) 
    dbExec(server, "INSERT INTO accounts (serial) VALUES (serialz) WHERE NOT EXISTS (SELECT serial FROM accounts WHERE serial = '"..serialz.."'") 
end 
addEventHandler("onPlayerJoin", root, getSerial) 

Share this post


Link to post

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.