V
V
Valeri Lavrov2019-06-11 04:10:56
MySQL
Valeri Lavrov, 2019-06-11 04:10:56

Why does MySQL return an empty value?

I am doing registration with duplication of data in the database for the game Multi Theft Auto.
inputs[1] and inputs[2] - login and password (only letters and numbers)
After registration, the user is written to the database. The rest of the default values ​​are set in the database.
If the write to the database is successful, then the script again accesses the database and searches for the player's data by nickname and gets the rest of the data, then spawns the user, gives him money, etc.
PROBLEM:

local sNewUser = query("SELECT * FROM `userinfo` WHERE `nickname` = '"..tostring(inputs[1]).."'")

This query returns an empty table
local sNewUser = query("SELECT * FROM `userinfo` WHERE `nickname` = 'test'")

If you make such a request, then everything returns.
Some documentation: https://wiki.multitheftauto.com/wiki/DbQuery
Screenshot of the database structure: https://yapx.ru/v/ETIP1
The code itself:
local dbHost = 'localhost'
local dbName = 'test'
local dbUsername = 'root'
local dbPassword = ''

function query(...)
  local queryHandle = dbQuery(dbConnect('mysql','host='..dbHost..';dbname='..dbName, dbUsername, dbPassword), ...)
  if (not queryHandle) then
    return nil
  end
  local rows = dbPoll(queryHandle, -1)
  return rows
end

local nA = addAccount(inputs[1], inputs[2]) -- добавляем юзера в игру
    local addA = query("INSERT INTO `userinfo` ('serial', 'nickname', 'password', 'team') VALUES ('"..tostring(getPlayerSerial(client)).."', '"..tostring(inputs[1]).."', '"..tostring(inputs[2]).."')") -- добавляем игрока в базу
      if (nA and addA) then -- если удачно добавился
        local sNewUser = query("SELECT * FROM `userinfo` WHERE `nickname` = '"..tostring(inputs[1]).."'") -- ищем игрока в базе по имени							
        if (sNewUser) then
            for _, row in ipairs(sNewUser) do -- перебираем данные из базы
              posX = row['posX'] -- позиция X
              posY = row['posY'] -- позиция Y
              posZ = row['posZ'] -- позиция Z
              usrHealth = row['health'] -- здоровье
              w11 = row['w11'] -- 11 слот оружия
              usrTeam = row['team'] -- команда
              usrSkinID = row['skin'] -- скин
              usrMoney = row['money'] -- деньги
              usrArmor = row['armor'] -- броня
              outputDebugString('posX: '..posX)
              outputDebugString('posY: '..posY)
              outputDebugString('posZ: '..posZ)
              outputDebugString('usrHealth: '..usrHealth)
              outputDebugString('w11: '..w11)
              outputDebugString('usrTeam: '..usrTeam)
              outputDebugString('usrSkinID: '..usrSkinID)
              outputDebugString('usrMoney: '..usrMoney)
              outputDebugString('usrArmor: '..usrArmor)
            end	
          logIn(client, nA, inputs[2]) -- логиним юзера
          spawnPlayer(client, posX, posY, posZ, 0, usrSkinID, 0, 0) -- спавн (игрок, позиция x, y, z, положение, скин, интерьер, измерение)
          setPlayerMoney (client, usrMoney) -- выдаем деньги
          setTimer (setElementHealth, 50, 1, client, usrHealth) -- выдаем здоровье
          setTimer (setPedArmor, 50, 1, client, usrArmor) -- выдаем армор
          triggerClientEvent(client, "onClientSuccess", client) -- передаем юзеру, что все ок
          --setPlayerTeam(client, getTeamFromName("User"))
          --setAccountData(nA, "cash", 250000)
          --triggerClientEvent(client, "onClientSuccess", client)
        else
          triggerClientEvent(client, "showErrorMessage", client, "Ошибка")
        end
      else
        triggerClientEvent(client, "showErrorMessage", client, "Ошибка при создании аккаунта.")
      end

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2019-06-11
@dollar

print("Login: " .. tostring(inputs[1]))
In general, place print in key places - you will immediately see what is happening.
Also, just in case, try:, otherwise the space at the end is attached or something. print(#tostring(inputs[1]))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question