• Logging connections of players to mysql Database
    1 replies, posted
Hi, I need help about logging connections of my players to a mysql database. I tried to add a Ip log but the gives nothing, null probably. I removed personnal informations. Working code, without ip logging: [CODE]DATABASE_HOST = "" DATABASE_PORT = 3306 DATABASE_NAME = "ip" DATABASE_USERNAME = "" DATABASE_PASSWORD = "" local MySQLOO = require( "mysqloo" ) DB_TUTORIAL = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT ) function ConnectToDatabase() print ("Connecting to Database") DB_TUTORIAL.onConnected = function() print("Database Connection Successful !") end DB_TUTORIAL.onConnectionFailed = function ( db, msg ) print("Connection to database failed") print( msg ) end DB_TUTORIAL:connect() end ConnectToDatabase() local function CheckDB() if ( DB_TUTORIAL:status() != mysqloo.DATABAS_CONNECTED ) then ConnectToDatabase() print("Database Connection restarted") end end timer.Create( "DB_CHECK", 5, 0, CheckDB) function SearchDatabase( ply ) if ply == nil then return end local query = DB_TUTORIAL:query( "SELECT id FROM users WHERE steamid = '" .. ply:SteamID() .. "';") if not query then return end query.onData = function( q, d ) if ( #query:getData() >= 1 ) then ply:ChatPrint("You are registered in the database as user #" .. tostring( d['id'] ) ) end end qurey.onError = function( db, err ) print("Search database: ", err ) end query:wait() query:start() end function AddPlayer( id ) local query = DB_TUTORIAL:query( "INSERT INTO users VALUES ('', '" .. id .. "');" ) query.onSuccess = function() print( "The new player " .. id .. " was logged!" ) end query.onError = function( db, err ) print("Add player err: ", err) end query:start() end hook.Add( "PlayerInitialSpawn", "AddplayerToDB", function( ply ) AddPlayer( ply:SteamID() ) end ) hook.Add( "PlayerSay", "CheckPlayerCommand", function( ply, txt, pub ) if string.sub(string.lower( txt ), 1, 4 ) == "!sql" then SearchDatabase( ply ) return "" end end )[/CODE] Code not working with ip logging. (bug: Adding nothing on the Table) [CODE]DATABASE_HOST = "" DATABASE_PORT = 3306 DATABASE_NAME = "ip" DATABASE_USERNAME = "" DATABASE_PASSWORD = "" local MySQLOO = require( "mysqloo" ) DB_TUTORIAL = mysqloo.connect( DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT ) function ConnectToDatabase() print ("Connecting to Database") DB_TUTORIAL.onConnected = function() print("Database Connection Successful !") end DB_TUTORIAL.onConnectionFailed = function ( db, msg ) print("Connection to database failed") print( msg ) end DB_TUTORIAL:connect() end ConnectToDatabase() local function CheckDB() if ( DB_TUTORIAL:status() != mysqloo.DATABAS_CONNECTED ) then ConnectToDatabase() print("Database Connection restarted") end end timer.Create( "DB_CHECK", 5, 0, CheckDB) function SearchDatabase( ply ) if ply == nil then return end local query = DB_TUTORIAL:query( "SELECT id FROM users WHERE steamid = '" .. ply:SteamID() .. "';") if not query then return end query.onData = function( q, d ) if ( #query:getData() >= 1 ) then ply:ChatPrint("You are registered in the database as user #" .. tostring( d['id'] ) ) end end qurey.onError = function( db, err ) print("Search database: ", err ) end query:wait() query:start() end function AddPlayer( id ) local query = DB_TUTORIAL:query( "INSERT INTO users VALUES ('', '" .. id .. "');" ) query.onSuccess = function() print( "The new player " .. id .. " was logged!" ) end query.onError = function( db, err ) print("Add player err: ", err) end query:start() end hook.Add( "PlayerInitialSpawn", "AddplayerToDB", function( ply ) AddPlayer( ply:SteamID() ) AddPlayer( ply:IPAddress() ) end ) hook.Add( "PlayerSay", "CheckPlayerCommand", function( ply, txt, pub ) if string.sub(string.lower( txt ), 1, 4 ) == "!sql" then SearchDatabase( ply ) return "" end end )[/CODE] Any help would be very nice ! :)
I undertand my problem now, but I dont know how to solve it. I tried: [CODE] function AddPlayer( id ) local query = DB_TUTORIAL:query( "INSERT INTO users VALUES ('', " .. id .. ");" ) query.onSuccess = function() print( "The new player " .. id .. " was logged!" ) end query.onError = function( db, err ) print("Add player err: ", err) end query:start() end hook.Add( "PlayerInitialSpawn", "AddplayerToDB", function( ply ) AddPlayer( ply:SteamID() .. ", " .. ply:IPAddress() ) [/CODE] [editline]9th April 2017[/editline] Finally got it ! I used the split function ! :rock: The code: [CODE] function AddPlayer( id ) steamid, ip = id:match("([^,]+),([^,]+)") --local query = DB_TUTORIAL:query( "INSERT INTO users VALUES ('', '" .. id .. "');" ) local query = DB_TUTORIAL:query( "INSERT INTO users VALUES ('', '" .. steamid .. "', '" .. ip .. "');" ) query.onSuccess = function() print( "The new player " .. id .. " was logged!" ) end query.onError = function( db, err ) print("Add player err: ", err) end query:start() end hook.Add( "PlayerInitialSpawn", "AddplayerToDB", function( ply ) AddPlayer( ply:SteamID() .. "," .. ply:IPAddress() ) end ) [/CODE]
Sorry, you need to Log In to post a reply to this thread.