Author Topic: Store Player Info in Database  (Read 1489 times)

0 Members and 1 Guest are viewing this topic.

Offline bigbadbeee

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Store Player Info in Database
« on: March 22, 2015, 01:22:14 PM »
here is my code
Code: [Select]
require( "mysqloo" )

DB_HOST = "localhost"
DB_PORT = 3306
DB_NAME = "badminmod"
DB_USERNAME = "root"
DB_PASSWORD = ""


function connectToDatabase()
databaseObject = mysqloo.connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME, DB_PORT)
databaseObject.onConnected = function() print("Connected to Database") end
databaseObject.onConnectionFailed = function() print("Database could not be found") end

end

connectToDatabase()

function checkQuery(query)
local playerInfo = query:getData()
if playerInfo[1] ~= nil then
return true
else
return false
end
end

function FirstJoinMysql( ply )
local query1 = databaseObject:query("SELECT * FROM bam_players WHERE ID = '" .. ply:SteamID() .. "'")
query1.onSuccess = function(q)

if not checkQuery(q) then
local query2 = databaseObject:query("INSERT INTO bam_players(ID, Name) VALUES ('" .. ply:SteamID() .. "'," .. ply:SteamName() .. ")")
query2.onSuccess = function(q) print("Your Info Was Stored") end
query2.onError = function(q,e) print("Your Info Was Not Stored") end
query2.start()
else
print("You are already created")
end
 end
 query1.OnError = function(q,e) print("Failed") end
 query1.start()
end
hook.Add( "PlayerInitialSpawn", "PlayerInitialSpawn", FirstJoinMysql )
   

I get this error
Code: [Select]
[ERROR] addons/badminmod/lua/autorun/server/bam.lua:40: attempt to index local 'query1' <a nil value>
1. v - addons/badminmod/lua/autorun/server/bam.lua:40
 2. unknown - lua/includes/modules/hook.lua:84

What Iam I doing wrong?

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2728
  • Karma: 430
    • |G4P| Gman4President
Re: Store Player Info in Database
« Reply #1 on: March 22, 2015, 01:34:59 PM »
Well, for starters... if you're using MySQLOO, it's:

query1:onError
query1:onSuccess
query1:start()

you also need to run databaseObject:connect() inside of your connection function.


Please look and see if you can find the post I've made here for using MySQL to do user authentication. That should have a working example of connecting to a database and doing queries.

Offline bigbadbeee

  • Newbie
  • *
  • Posts: 2
  • Karma: 0
Re: Store Player Info in Database
« Reply #2 on: March 22, 2015, 01:47:02 PM »
Thank I will try it and Try to find your post