A
A
AndRussia2020-10-09 09:12:38
Lua
AndRussia, 2020-10-09 09:12:38

MTA (Multi Theft Auto) LUA - What's wrong with my database query?

Hello, I don't understand what is my mistake. What have I done wrong?
The bottom line is this: there is a connection to the database and sql queries (code 1). I'm trying to get a login and password into a variable, but the console gives an error (screenshot 1)
Code 1:

function connect()
    db = dbConnect( "mysql", "dbname=users;host=127.0.0.1;charset=utf8", "root", "" )
    if (not db) then
        outputDebugString("Error: Failed to establish connection to the MySQL database server")
    else
    	dbExec(db, "CREATE TABLE IF NOT EXISTS users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, login VARCHAR(32), password VARCHAR(24), status INT, hp INT, money INT, level INT)")
        outputDebugString("Success: Connected to the MySQL database server")
    end
end
addEventHandler("onResourceStart",resourceRoot, connect)

function playerRegister(login, password)
  local player = source
  local mylogin = getAccount(login)
  local mypassword = getAccount(password)
  if not getAccount(login) then
    addAccount(login, password)
    dbExec(db, "INSERT INTO users(login, password, status, hp, money, level) VALUES("..mylogin..", "..mypassword..", 1, 100, 1500, 16)")
    outputChatBox('Аккаунт успешно создан.', player)
  else
    outputChatBox('Аккаунт с таким логином существует!', player)
  end
end
addEvent('playerRegister', true)
addEventHandler('playerRegister', root, playerRegister)

function playerLogin(login, password)
  local player = source
  local mylogin = getAccount(login)
  local mypassword = getAccount(password)
  local dblogin = dbQuery(db, "SELECT `login` FROM `users` WHERE login = "..mylogin.."")
  local dbpassword = dbQuery(db, "SELECT `password` FROM `users` WHERE login = "..mylogin.."")
  if (dblogin == login) then
    if (dbpassword == password) then
      logIn(player, getAccount(login, password), password)
      outputChatBox('Добро пожаловать!', player)
      triggerClientEvent(player, 'closePanel', player)
      setElementFrozen(source, false)
    end
  else
    outputChatBox('Неправильный логин или пароль!', player)
  end
end
addEvent('playerLogin', true)
addEventHandler('playerLogin', root, playerLogin)

addEventHandler('onPlayerJoin', root, function()
  triggerClientEvent(source, 'openPanel', source)
  setElementFrozen(source, true)
end)
addEventHandler('onPlayerLogout', root, function()
  triggerClientEvent(source, 'openPanel', source)
  setElementFrozen(source, true)
end)

Screenshot 1:
5f7fff46563a0423123441.png
I can not choose the difficulty of the question, I will put "Medium".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2020-10-09
@AndRussia

In Lua, only numbers can be glued to strings with automatic type conversion.
Other types need to be manually converted to strings.
So instead of: You need:
", " .. mypassword .. ", 1, 100, 1500, 16)"

", " .. tostring(mypassword) .. ", 1, 100, 1500, 16)"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question