Jump to content

MySQL thing. Need help


an4rei

Recommended Posts

Hello , I need some help with next thing:

Here is an example:

function reward() 
--gets the player's serial and searches the mysql table for it  
--if the serial exists in mysql table , will outputchatbox something 
--if the serial doesn't exists in mysql table will do something like GivePlayerMoney + adds the serial in the mysql table 
addCommandHandler("gift" , reward) 

I hope you have understood what I want to make , 1 use command for each player.

Thanks.

Edited by Guest
Link to comment

Untested, but should somewhat show you how it works

function reward(player) 
  --gets the player's serial and searches the mysql table for it 
  local serial = getPlayerSerial(player) 
  local query = dbQuery(handler,"SELECT serial FROM rewardtable WHERE serial=?",serial) -- handler is your mysql connection with dbConnect. Might be a bit different with your table 
  local data = dbPoll(query,-1) -- query the results to get the actual serial 
  dbFree(query) 
  local info = data[1] -- get the first result 
  if info and info["serial"] then -- if there is at least 1 result with this serial 
    -- will outputchatbox something 
    outputChatBox("Yo man! You already got the gift!",player,255,255,255,true) 
  else 
    --if the serial doesn't exists in mysql table will do something like GivePlayerMoney + adds the serial in the mysql table 
    dbExec(handler,"INSERT INTO rewardtable (serial,playerName) VALUES (?,?)",serial,getPlayerName(player)) -- execute to insert into the table. The query can be a bit different depending on your database. Added playerName example in case you need it 
    givePlayerMoney(player,10000) -- give the cash 
    outputChatBox("Shut up and take my money!",player,255,255,255,true) 
  end 
end 
addCommandHandler("gift",reward) 

Edited by Guest
Link to comment
local cn = dbConnect ( string databaseType, string host [, string username = "", string password = "", string options = "" ] ) 
function give_r(pl ) 
      local theSerial = getPlayerSerial(pl) 
      local qh = dbQuery( cn, "SELECT * FROM users WHERE player_serial=?",theSerial ) 
      local result, rows, last_insert_id = dbPoll ( qh, -1 ) 
  
    if(result)then 
       if(rows > 0)then 
           --if the serial exists in mysql table , will outputchatbox something 
      else 
           dbExec(cn,"INSERT INTO users (player_serial) VALUES (?)",theSerial) 
          --if the serial doesn't exists in mysql table will do something like GivePlayerMoney + adds the serial in the mysql table 
      end 
  
    end 
end 
addCommandHandler( "reward", give_r ) 
  

Link to comment

Thank you both , I've used first example and it worked.

Untested, but should somewhat show you how it works
function reward(player) 
  --gets the player's serial and searches the mysql table for it 
  local serial = getPlayerSerial(player) 
  local query = dbQuery(handler,"SELECT serial FROM rewardtable WHERE serial=?",serial) -- handler is your mysql connection with dbConnect. Might be a bit different with your table 
  local data = dbPoll(query,-1) -- query the results to get the actual serial 
  dbFree(query) 
  local info = data[1] -- get the first result 
  if info and info["serial"] then -- if there is at least 1 result with this serial 
    -- will outputchatbox something 
    outputChatBox("Yo man! You already got the gift!",player,255,255,255,true) 
  else 
    --if the serial doesn't exists in mysql table will do something like GivePlayerMoney + adds the serial in the mysql table 
    dbExec(handler,"INSERT INTO rewardtable (serial,playerName) VALUES (?,?)",serial,getPlayerName(player)) -- execute to insert into the table. The query can be a bit different depending on your database. Added playerName example in case you need it 
    givePlayerMoney(player,10000) -- give the cash 
    outputChatBox("Shut up and take my money!",player,255,255,255,true) 
  end 
end 
addCommandHandler("gift",reward) 

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