Jump to content

Simple MySQL query.


TrickyTommy

Recommended Posts

Hi. I wan't to make a mini account system, but don't really know, how. I already can insert stuff to my database, but can't check if they exist from game, but php my admin.

This is how my script looks like:

Connection = dbConnect( "mysql", "dbname=texas;host=127.0.0.1", "root", "", "share=1" )

if Connection then
	outputServerLog ("Connection succeeded!")
else
	outputServerLog ("Connection failure!")
end

function Insert(Client, Command, Username, Password)
	if Password then
		local Query = dbQuery(Connection, "INSERT INTO users (username, password) VALUES (?, ?)", Username, Password)
		if Query then
			outputDebugString ("Succesful query!")
		else
			outputDebugString ("Unsuccesful query!")
		end
	end
end
addCommandHandler ("insert", Insert)

function Call(Client, Command, Username, Password)
	if Password then
		--If username and password exists in database, notify the user that given Username and Password parameter is correct.
	end
end
addCommandHandler ("call", Call)

In function named "Call" i want a very simple thing: there are 2 parameters inside, Username and Password. If there is a row, in wich the password and username is the same as the parameter, i want it to like output a message, else make the user aware that the given details are wrong.

Please, be so kind and help me, because i have no idea how should i do it, + add comments if possible.

Link to comment
function Call(Client, Command, Username, Password)
	local query = dbQuery(Connection, "SELECT `username`, `password` FROM users WHERE username = '"..Username.."' AND password = '"..Password.."';")
	local result = dbPoll(query, -1)
	if result > 0 then
		-- Match found success
	end
end

 

Edited by Taka
  • Thanks 1
Link to comment
function Call(Client, Command, Username, Password)
	local query = dbQuery(Connection, "SELECT * FROM users WHERE username = '"..Username.."' AND password = '"..Password.."';")
	local result = dbPoll(query, -1)
	if result > 0 then
		-- Match found success
		for row, rowData in ipairs(result) do
			-- rowData['column name']
			outputChatBox(rowData['username'])
			outputChatBox(rowData['password'])
		end
	end
end

 

  • Thanks 1
Link to comment
2 hours ago, TrickyTommy said:

if Result > 0 then

Debugscript said that "attempt to compare number with table" what is wrong?

Oops.

function Call(Client, Command, Username, Password)
	local query = dbQuery(Connection, "SELECT * FROM users WHERE username = '"..Username.."' AND password = '"..Password.."';")
	local result, num_rows = dbPoll(query, -1)
	if num_rows > 0 then
		-- Match found success
		for row, rowData in ipairs(result) do
			-- rowData['column name']
			outputChatBox(rowData['username'])
			outputChatBox(rowData['password'])
		end
	end
end

 

  • Thanks 1
Link to comment
1 hour ago, TrickyTommy said:

thank you guys, i think i have one last question. what method should i use for getting the player's account id from database, and after set it as his element data?

function Call(Client, Command, Username, Password)
	local query = dbQuery(Connection, "SELECT * FROM users WHERE username = '"..Username.."' AND password = '"..Password.."';")
	local result = dbPoll(query, -1)
	if #result > 0 then
		-- Match found success
		for row, rowData in ipairs(result) do
			setElementData(Client, "id", tonumber(rowData['id']))
		end
	end
end

 

  • Thanks 1
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...