John Smith Posted April 3, 2015 Share Posted April 3, 2015 hi i wrote this code function addPlayerInTeam(player,cmd,targetElement,team,rank) if exports.admin:isPlayerAdmin(player) then if not team then outputChatBox("[/setplayerteam] team is missing at argument 2!",player,255,0,0) return end if not rank then outputChatBox("[/setplayerteam] rank is missing at argument 3!",player,255,0,0) return end local targetElement = getPlayerFromName(targetElement) if targetElement then local isPlayerInClan = dbExec(database,"SELECT * FROM "..team.." WHERE memberSerial=?",getPlayerSerial(targetElement)) if not isPlayerInClan then local result = dbExec(database,"INSERT INTO "..team:lower().." ( memberName, memberSerial, memberRank ) VALUES (?,?,?)",getPlayerName(targetElement):gsub("#%x%x%x%x%x%x",""),getPlayerSerial(targetElement),rank) outputChatBox("Transferring player to sql database into "..team.." team...",player,0,0,255) outputChatBox("Result: "..tostring(result)) else outputChatBox("WARNING! Player is already in that clan!",player,255,0,0) end else outputChatBox("[/setplayerteam] player not found.",player,255,0,0) end end end addCommandHandler("setplayerteam",addPlayerInTeam) i am pretty new at sql and i didnt know any other way to check if something exists rather than SELECT * FROM something WHERE memberSerial=?,serial please help if you know sql Link to comment
Castillo Posted April 3, 2015 Share Posted April 3, 2015 You need to use dbQuery + dbPoll to check if something is already in the database. Link to comment
John Smith Posted April 3, 2015 Author Share Posted April 3, 2015 i changed lines to this local isPlayerInClan = dbQuery(database,"SELECT * FROM "..team.." WHERE memberSerial=?",getPlayerSerial(targetElement)) local theResult = dbPoll(isPlayerInClan,-1) if not theResult then --dbExec(...) else --output warning player found --... still it always says player already in database i am not sure how do i debug this, result returns a table, i thought it returns a boolean.. idk what to do now Link to comment
Castillo Posted April 3, 2015 Share Posted April 3, 2015 local theResult, numRows = dbPoll ( isPlayerInClan, -1 ) if ( numRows == 0 ) then -- your code end Link to comment
John Smith Posted April 3, 2015 Author Share Posted April 3, 2015 thanks but umm i have other problem.. in my code theres variable team which is transferred over command i have inserted myself into team a and it returned true if i want to put myself again into a it doesnt allow it - nice however if i want to put myself in team b as well, it wont allow me Link to comment
Castillo Posted April 3, 2015 Share Posted April 3, 2015 I don't understand what do you mean. Link to comment
John Smith Posted April 3, 2015 Author Share Posted April 3, 2015 i am in a progress of making team/clan manager which will later consist of a gui where i or someone else would put people in other teams or in my own team i have created a command like in above code just for testing with SQL since im pretty new at it, still learning with above code+ some editions from you(which you posted) i got a script which will successfully put me in my sql table team of choice however when i put myself once in any team, i cant put myself in any other team since it outputs to chatbox "player is already in a clan" and i dont know how to solve this as im new to sql.. Link to comment
John Smith Posted April 3, 2015 Author Share Posted April 3, 2015 i am sorry.. i dont know how but after i have restarted the resource couple of times and tried debugging what is the numRows returning, it somehow got fixed by itself thanks for help solidsnake14! Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now