[lua]
function darkrp_InsertMysql(query,darkrp_dbConnection)
if not query then return '' end
local function QueryError(error_message)
print("~[Mysql]~ Error - {"..error_message.."}")
end
query1.onFailure = QueryError(error_message)
query1:start()
end
function darkrp_ConnectMysql()
local function ConnectionSuccess()
print("~[Mysql]~ Connected Successfully.")
darkrp_MYSQL_CONN = true
end
local function ConnectionFailed(e)
print("~[Mysql]~ CRITICAL! CONNECTION FAILED! ["..e.."]")
end
darkrp_dbConnection = mysqloo.connect(darkrp_MYSQL_HOST, darkrp_MYSQL_USER, darkrp_MYSQL_PASS, darkrp_MYSQL_DATA, 3306)
darkrp_dbConnection.onConnected = ConnectionSuccess()
darkrp_dbConnection.onConnectionFailed = ConnectionFailed(e)
darkrp_dbConnection:connect()
end
hook.Add("Initialize","InitializeDB","ConnectMysql")
function darkrp_AddMoney()
local query = "INSERT INTO darkrp_money (`steamid`, `amount`)VALUES ('id','5')"
darkrp_InsertMysql(query,darkrp_dbConnection)
end
concommand.Add("addit",darkrp_AddMoney)
[/lua]
Error: attempt to concatenate local 'error_message' (a nil value) @ line 4
Don't concat the error message with a string; simply print it.
[lua]
local function QueryError(error_message)
print("~[Mysql]~ Error", error_message)
end
[/lua]
You might be able to cast it to a string, not sure though (never tried it).
I fixed that but now I get this error...
Error: '<name>' expected near '(' @ line 8
[lua]
function darkrp_InsertMysql(thequery,darkrp_dbConnection)
if not thequery then return '' end
local function QueryError(error_message)
print("~[Mysql]~ Error - {"..error_message.."}")
end
print(thequery) -- Prints "INSERT INTO darkrp_money (`steamid`, `amount`)VALUES ('id','5')"
local query1 = darkrp_dbConnection:query(thequery) -- Error Here
query1.onError = function(Q,E) print("Q1") print(E) end
query1:start()
end
function darkrp_ConnectMysql()
local function ConnectionSuccess()
print("~[Mysql]~ Connected Successfully.")
darkrp_MYSQL_CONN = true
end
local function ConnectionFailed(e)
print("~[Mysql]~ CRITICAL! CONNECTION FAILED! ["..e.."]")
end
darkrp_dbConnection = mysqloo.connect(darkrp_MYSQL_HOST, darkrp_MYSQL_USER, darkrp_MYSQL_PASS, darkrp_MYSQL_DATA, 3306)
darkrp_dbConnection.onConnected = ConnectionSuccess()
darkrp_dbConnection.onConnectionFailed = ConnectionFailed(e)
darkrp_dbConnection:connect()
end
hook.Add("Initialize","InitializeDB",darkrp_ConnectMysql)
function darkrp_AddMoney()
local query = "INSERT INTO darkrp_money (`steamid`, `amount`)VALUES ('id','5')"
darkrp_InsertMysql(query,darkrp_dbConnection)
end
concommand.Add("addit",darkrp_AddMoney)
[/lua]
First off, use a different variable name for the second argument of the darkrp_InsertMysql function. You are passing the query and a handle to the database connection (line 34), which is fine, but you should not use the same name for the argument, especially since the connection was not defined locally.
So do something like this:
[lua]
function darkrp_InsertMysql(thequery, hConn)
if not thequery then return '' end
local function QueryError(error_message)
print("~[Mysql]~ Error - {"..error_message.."}")
end
local query1 = hConn:query(thequery)
query1.onError = function(Q,E) print("Q1") print(E) end
query1:start()
end
[/lua]
Not sure if this will fix it, but it's worth a shot. Plus, it's not bad practice.
Yea but where am I going to get hConn?
[QUOTE=zzaacckk;26956641]Yea but where am I going to get hConn?[/QUOTE]
mysqloo.connect(...)
so I just do hConn = darkrp_dbConnection??
[QUOTE=zzaacckk;26956865]so I just do hConn = darkrp_dbConnection??[/QUOTE]
Yes.
MySQLOO breaks on some machines, but that's not the reason to completely disregard it.
[lua]
function darkrp_ConnectMysql()
local function ConnectionSuccess()
print("~[Mysql]~ Connected Successfully.")
darkrp_MYSQL_CONN = true
end
local function ConnectionFailed(e)
print("~[Mysql]~ CRITICAL! CONNECTION FAILED! ["..e.."]")
end
darkrp_dbConnection = mysqloo.connect(darkrp_MYSQL_HOST, darkrp_MYSQL_USER, darkrp_MYSQL_PASS, darkrp_MYSQL_DATA, 3306)
darkrp_dbConnection.onConnected = ConnectionSuccess()
darkrp_dbConnection.onConnectionFailed = ConnectionFailed(e)
darkrp_dbConnection:connect()
theConn = darkrp_dbConnection
end
hook.Add("Initialize","InitializeDB",darkrp_ConnectMysql)
function darkrp_InsertMysql(thequery,theConn)
if not thequery then return '' end
local function QueryError(error_message)
print("~[Mysql]~ Error - {"..error_message.."}")
end
print(thequery)
local query1 = theConn:query(thequery) -- Error on this line *^*^*^*^*^*^*^*^*
query1.onError = function(Q,E) print("Q1") print(E) end
query1:start()
end
function darkrp_AddMoney()
local query = "INSERT INTO darkrp_money (`steamid`, `amount`)VALUES ('id','5')"
darkrp_InsertMysql(query,theConn)
end
concommand.Add("addit",darkrp_AddMoney)
[/lua]
Error: attempt to index local 'theConn' (a nil value) @ line 28
Hmm....
tmysql
No, you misunderstood what I was trying to say. You call the function on line 36 (new code). The second argument you give to the function is the connection handle. Since you [b]give the connection handle to the function[/b], it will also be the second argument in the function itself. This is what I meant:
[lua]
function darkrp_InsertMysql(thequery, hConn) -- hConn is a reference to the handle
if not thequery then return '' end
local function QueryError(error_message)
print("~[Mysql]~ Error ", error_message)
end
local query1 = hConn:query(thequery)
query1.onError = function(Q,E) print("Q1") print(E) end
query1:start()
end
function darkrp_ConnectMysql()
local function ConnectionSuccess()
print("~[Mysql]~ Connected Successfully.")
darkrp_MYSQL_CONN = true
end
local function ConnectionFailed(e)
print("~[Mysql]~ CRITICAL! CONNECTION FAILED! ["..e.."]")
end
darkrp_dbConnection = mysqloo.connect(darkrp_MYSQL_HOST, darkrp_MYSQL_USER, darkrp_MYSQL_PASS, darkrp_MYSQL_DATA, 3306) -- this is your connection handle
darkrp_dbConnection.onConnected = ConnectionSuccess()
darkrp_dbConnection.onConnectionFailed = ConnectionFailed(e)
darkrp_dbConnection:connect()
end
hook.Add("Initialize","InitializeDB",darkrp_ConnectMysql)
function darkrp_AddMoney()
local query = "INSERT INTO darkrp_money (`steamid`, `amount`) VALUES ('id','5')"
darkrp_InsertMysql(query, darkrp_dbConnection) -- here you give the handle to the function
end
concommand.Add("addit",darkrp_AddMoney)
[/lua]
Error: attempt to index local 'hConn' (a nil value)
Using the code you gave me nonSense
Please remove this post.
Sorry, you need to Log In to post a reply to this thread.