Jump to content

[HELP] Export Function ...


Recommended Posts

Hello there,

I've been scripting a login script working on mysql.

Until now it quite sucseed but, i'm having some a problem.

The problem is i want to create a export / custom function so it would be easier to

use the function in syntaxes etc.

  
function getUsername(source) 
local handler = mysql_connect("localhost", "root", "BLABLABLA", "BLABLABLA") 
local result = mysql_query(handler, "SELECT * FROM users WHERE serial = '"..getPlayerSerial( source ).."' AND online = '1';") -- Execute the query 
if (result) then 
  while true do 
    local row = mysql_fetch_assoc(result) 
    if (not row) then break end 
  
   --[[ outputChatBox(row["username"].." is your username.", source, 0, 255, 0) ]]-- 
  end 
  mysql_free_result(result) -- Free the result 
end 
end 
addEvent( "getUsername", true ) 
addEventHandler( "getUsername", getRootElement(), getUsername ) 
  

As you guys can see i've been struggling to make a function that get's the username of the player

But now i want to use it in mysql syntaxes and more thing so i need to create an export or a custom function.

I already tried myself but it did not succes. I did add the function in meta.xml as export

And then i used the export like this:

    local getUsername1 = exports.login_mysql:getUsername(source) 
        local CheckPlayer = mysql_query(handler,"SELECT * FROM users WHERE username = '"..getUsername1.."' AND online = '1';")  

But it did not work and gave me the error: ERROR: login_mysql\login_server.lua:71: attempt to concatenate local 'getUsername1' (a nil value). I've searched the forums for days to look for an explaination or fix

but again, i did not sucssed so now i'm asking somebody for help... and an explaination.

Thanks in forward,

Greetz SyntaxError

Link to comment

Do not connect to MySQL every time you trigger!

Also, I suggest you to switch to:

dbConnect 
dbQuery 
dbPoll 
dbFree 

This might work:

function getUsername(source) 
local handler = mysql_connect("localhost", "root", "BLABLABLA", "BLABLABLA") 
local result = mysql_query(handler, "SELECT * FROM users WHERE serial = '"..getPlayerSerial( source ).."' AND online = '1';") -- Execute the query 
if (result) then 
  while true do 
    local row = mysql_fetch_assoc(result) 
    if (not row) then break end 
  
   --[[ outputChatBox(row["username"].." is your username.", source, 0, 255, 0) ]]-- 
  end 
  return row 
  mysql_free_result(result) -- Free the result 
end 
end 
addEvent( "getUsername", true ) 
addEventHandler( "getUsername", getRootElement(), getUsername ) 

Link to comment
Do not connect to MySQL every time you trigger!

Also, I suggest you to switch to:

dbConnect 
dbQuery 
dbPoll 
dbFree 

This might work:

function getUsername(source) 
local handler = mysql_connect("localhost", "root", "BLABLABLA", "BLABLABLA") 
local result = mysql_query(handler, "SELECT * FROM users WHERE serial = '"..getPlayerSerial( source ).."' AND online = '1';") -- Execute the query 
if (result) then 
  while true do 
    local row = mysql_fetch_assoc(result) 
    if (not row) then break end 
  
   --[[ outputChatBox(row["username"].." is your username.", source, 0, 255, 0) ]]-- 
  end 
  return row 
  mysql_free_result(result) -- Free the result 
end 
end 
addEvent( "getUsername", true ) 
addEventHandler( "getUsername", getRootElement(), getUsername ) 

Nope didn't work.... Still thanks for your time and effort i appreciate it.

Link to comment

As I said, use DB functions.

local handler = dbConnect("mysql", "dbname=YOUR DATABASE;host=YOUR IP", "YOUR USERNAME", "YOUR PASSWORD") 
function getUsername(source) 
    local query = dbQuery(handler, "SELECT * FROM users WHERE serial=? AND online='1'", getPlayerSerial(source)) 
    if query then 
        local result = dbPoll(handler, -1) 
        if result then 
            return result 
        end 
    dbFree(query) 
    end 
    return false 
end 

And it's used like this:

local username = exports.login_mysql:getUsername(player) 
outputChatBox(username[1]["username"]) 

Link to comment
As I said, use DB functions.
local handler = dbConnect("mysql", "dbname=YOUR DATABASE;host=YOUR IP", "YOUR USERNAME", "YOUR PASSWORD") 
function getUsername(source) 
    local query = dbQuery(handler, "SELECT * FROM users WHERE serial=? AND online='1'", getPlayerSerial(source)) 
    if query then 
        local result = dbPoll(handler, -1) 
        if result then 
            return result 
        end 
    dbFree(query) 
    end 
    return false 
end 

And it's used like this:

local username = exports.login_mysql:getUsername(player) 
outputChatBox(username[1]["username"]) 

Thanks for your help i already figured it out with another friend.

But still many thanks to you. I truly apperciate your time and effort.

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