golanu21 Posted November 9, 2013 Share Posted November 9, 2013 --SERVER SIDE -- MySQL Connection local host = "*****"; local dbname = "***"; local username = "***"; local password = "****"; function connectToDB() rpg_db = dbConnect("mysql", "dbname="..dbname..";host="..host, username, password, "autoreconnect=1") return rpg_db end function creeazaTabel() rpg_db = connectToDB() local characters = dbExec( rpg_db, "CREATE TABLE IF NOT EXISTS characters (id INT PRIMARY KEY NOT NULL, nume CHAR(50) NOT NULL, model INT NOT NULL, health REAL NOT NULL, posx REAL NOT NULL, posy REAL NOT NULL, posz REAL NOT NULL)") local accounts = dbExec( rpg_db, "CREATE TABLE IF NOT EXISTS accounts (id INT PRIMARY KEY NOT NULL, nume CHAR(50) NOT NULL, parola CHAR(50) NOT NULL, level REAL NOT NULL, rp REAL NOT NULL, drivinglicense REAL NOT NULL, weaponlicense REAL NOT NULL)") end addEventHandler("onResourceStart", getRootElement(getThisResource()), creeazaTabel) and an export function in meta.xml function="connectToDB" type="server"/> and idk how make that: i make an other script [account-system] and when the player click on "Register" button for example, to do an dbExec on "accounts" table Link to comment
Castillo Posted November 9, 2013 Share Posted November 9, 2013 Connecting to the database everytime you need to make a query is a really bad thing. Link to comment
tosfera Posted November 9, 2013 Share Posted November 9, 2013 The best thing to do ( in my eyes ) is to create a database connection whenever your database platform ( if you want to go that deep into the base ofc ) starts. Then create a function for mysql_escape_string, mysql_query, mysql_num_rows etc. In the same resource. Export these functions to some good looking functions. Like; query ( mysql_query ), escape ( mysql_escape_string ). In the database platform, you should use the full function with your db connection. Like this; local db; addEventHandler ( "onResourceStart", getResourceRootElement ( getThisResource() ), function () db = mysql_connect ( "localhost", "username", "password", "database" ); if ( db ) then outputDebugString ( "MySQL connection started succesfully" ); else outputDebugString ( mysql_error( db ) ); end end ); function escape ( str ) return mysql_escape_string ( db, str ); end function insert ( str ) local query = mysql_insert ( db, str ); if ( query ) then outputDebugString ( "MySQL query executed succesfully" ); else outputDebugString ( mysql_error( db ) ); end mysql_free_result ( query ); end Don't forget to export the functions; function="escape" /> function="insert" /> To call an exported function, there are a few ways. The easiest way is; local mysql = exports[""]; After that you can just call an function; addCommandHandler ( "escape", function ( thePlayer, theCommand, str ) outputChatBox ( "Escaped string is: ".. mysql:escape ( str ) ); end ); 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