BigBrother Posted October 13, 2013 Share Posted October 13, 2013 Hello, I'm beggining my server, and for the moment there is two main resources : sql and account In the SQL, I have a sql.lua file where in i'm connecting to the database and a meta.xml where in i'm exporting the function which enables me to connect to mysql. SQL.LUA function connectMySQL() db = mysql_connect(db_host, db_login, db_pass, db_name, db_port) if (db) then -- The connection failed outputDebugString("Connected.") triggerEvent("onMySQLConnected", root) else outputDebugString("Unable to connect to the MySQL server") end end addEventHandler("onResourceStart", resourceRoot, connectMySQL) meta.xml (of the SQL resource) <meta> <info author="BigBrother" version="1.0" type="gamemode" name="WeRoleplay"/> <!-------- SCRIPTS --------> <script src="sql.lua" type="server" /> <!-------------------------> <!-------- CONFIGS --------> <!-------------------------> <export function="connectMySQL" http="true" /> </meta> And a second resource named account where in I have a meta.xml and a login.lua file LOGIN.LUA function isPlayerRegistred() joinPlayerName = getPlayerName(source) db_import = exports.sql:connectMySQL() local result = mysql_query(db_import, "SELECT * FROM users WHERE username = '"..joinPlayerName.."'") local row = mysql_fetch_row(result) print(row) if (row == nil) then outputChatBox(" ", source, 255, 255, 255, true) outputChatBox("Votre compte n'est pas enregistré. Veuillez vous inscrire sur notre plateforme web.", source, 185, 74, 72, true) else spawnPlayer(source, 0.0, 0.0, 3.0) fadeCamera(source, true) setCameraTarget(source) outputChatBox(" ", source, 255, 255, 255, true) outputChatBox("Bienvenue sur WeRoleplay, #FFFFFF"..joinPlayerName, source, 153, 153, 153, true) outputChatBox("Veillez à bien respecter les règles du serveur.", source, 153, 153, 153, true) outputChatBox(" ", source, 255, 255, 255, true) mysql_free_result(result) end end addEventHandler("onPlayerJoin", root, isPlayerRegistred) Meta.xml (of the account resource) <meta> <info author="BigBrother" version="1.0" type="resource" name="WeRoleplay"/> <!-------- SCRIPTS --------> <script src="login.lua" type="server" /> <!-------------------------> <!-------- CONFIGS --------> <!-------------------------> </meta> The two lua.sql files used to be regroup in one file, so it used to work, but now I have this error : [2013-10-13 20:05:12] ERROR: [WeRoleplay]\account\login.lua:16: bad argument #1 to 'mysql_query' (mysqlHandler expected, got nil) Could you Help me, please. - BigBrother Link to comment
Castillo Posted October 13, 2013 Share Posted October 13, 2013 function connectMySQL ( ) if ( not db ) then db = mysql_connect ( db_host, db_login, db_pass, db_name, db_port ) if ( db ) then outputDebugString ( "Connected." ) triggerEvent ( "onMySQLConnected", root ) return db else outputDebugString ( "Unable to connect to the MySQL server" ) return false end else return db end end addEventHandler ( "onResourceStart", resourceRoot, connectMySQL ) Link to comment
Chopper Posted October 13, 2013 Share Posted October 13, 2013 Why dont you just use this. mysql_host = "127.0.0.1" mysql_user = "root" mysql_pass = "yourpass" mysql_db = "yourdb" db_import = mysql_connect(mysql_host,mysql_user,mysql_pass,mysql_db) Much easier. but you can also try using sql:query 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