• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Select query returning "true" -- How to get to value?

Dangnoob

Member
Joined
Jun 8, 2008
Messages
105
Solutions
2
Reaction score
12
Currently when i run this command it is return "y" as "true" It is 0 currently, how do i get it to display the number, not the true or false?? I just tried
local v = getDataInt('y') .. it didnt work :/ Thanks for any help in advance


Lua:
function onSay(cid, words, param, channel)
    if(param == '') then
        local x = getPlayerStorageValue(cid,69693)
        local y = db.executeQuery("SELECT `currency` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(y))
        return true
    end
    return true
end
 
Currently using this script to try and try this to make it a number, im not sure what to do honestly.

Lua:
function onSay(cid, words, param, channel)
    if(param == '') then
        local x = getPlayerStorageValue(cid,69693)
        local y = db.executeQuery("SELECT `currency` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
    if(y == false) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "it was false")
            return TRUE
    end
    if(y == true) then
            doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "it was true")
            return TRUE
    end
        else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, y)
        return TRUE
    end
end
 
Try:
Lua:
y:getDataInt("currency")

for the query itself I think it should be db.getResult instead of db.executeQuery
 
This is how you use the db.storeQuery (only for "SELECT" keyword):

Lua:
-- Return true if has at least 1 result
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    result.free(resultId) -- Free memory
    return true -- Found
  end
  return false -- Not found
end

-- Gets the first result id
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    local id = result.getDataInt(resultId, "id") -- id of result (example data)
    result.free(resultId) -- Free memory
    return id -- id of first found result
  end
  return -1 -- Not found
end

-- Executes for each result
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    repeat
      local id = result.getDataInt(resultId, "id") -- id of actual result (example data)
      doSomething(id) -- Do something with this id
    until not result.next(resultId)
    result.free(resultId) -- Free memory
    return true -- Found at least 1 result and executed "doSomething" on each id of them
  end
  return false -- Not found and did nothing
end
 
This is how you use the db.storeQuery (only for "SELECT" keyword):

Lua:
-- Return true if has at least 1 result
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    result.free(resultId) -- Free memory
    return true -- Found
  end
  return false -- Not found
end

-- Gets the first result id
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    local id = result.getDataInt(resultId, "id") -- id of result (example data)
    result.free(resultId) -- Free memory
    return id -- id of first found result
  end
  return -1 -- Not found
end

-- Executes for each result
function func()
  local resultId = db.storeQuery("SELECT <YOUR QUERY STRING HERE>")
  -- If found at least 1 result
  if resultId ~= false then
    repeat
      local id = result.getDataInt(resultId, "id") -- id of actual result (example data)
      doSomething(id) -- Do something with this id
    until not result.next(resultId)
    result.free(resultId) -- Free memory
    return true -- Found at least 1 result and executed "doSomething" on each id of them
  end
  return false -- Not found and did nothing
end


Okay after trying this, it is now posting "nil" instead of true/false



Lua:
function onSay(cid, words, param, channel)
    if(param == '') then
        local x = getPlayerStorageValue(cid,69693)
        local resultId = db.storeQuery("SELECT `currency` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
            if resultId ~= false then
                local id = result.getDataInt(resultId, "id") -- id of result (example data)
                result.free(resultId)
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))                -- Free memory
                return id                -- id of first found result
            end
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))
        return -1 -- Not found
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))
    return TRUE
end
 
Okay after trying this, it is now posting "nil" instead of true/false



Lua:
function onSay(cid, words, param, channel)
    if(param == '') then
        local x = getPlayerStorageValue(cid,69693)
        local resultId = db.storeQuery("SELECT `currency` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
            if resultId ~= false then
                local id = result.getDataInt(resultId, "id") -- id of result (example data)
                result.free(resultId)
                doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))                -- Free memory
                return id                -- id of first found result
            end
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))
        return -1 -- Not found
    end
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, tostring(id))
    return TRUE
end
There is no "id" in that query. You only are selecting "currency", if you want to return the currency then do:
Lua:
result.getDataInt(resultId, "currency")

If you want to get id of the player then do:
Lua:
db.storeQuery("SELECT `id` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
 
There is no "id" in that query. You only are selecting "currency", if you want to return the currency then do:
Lua:
result.getDataInt(resultId, "currency")

If you want to get id of the player then do:
Lua:
db.storeQuery("SELECT `id` FROM `players` WHERE `name` = " .."\"".. x .. "\"")
Thanks for the response. I will try this in the morning
 
Thanks for the response. I will try this in the morning

Lua:
local storageId = 69693

function onSay(cid, words, param, channel)
  if param ~= '' then
    return true
  end

  local storageValue = getPlayerStorageValue(cid, storageId)
  local resultId     = db.storeQuery(string.format("SELECT `currency` FROM `players` WHERE `name` = %s ;", storageValue))

  -- Found at least 1 result
  if resultId ~= false then
    local currency = result.getDataInt(resultId, "currency")
    result.free(resultId)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, string.format("Currency: %d", currency))
  else
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Oops! No results were found!")
  end
  return true
end
 
Lua:
local storageId = 69693

function onSay(cid, words, param, channel)
  if param ~= '' then
    return true
  end

  local storageValue = getPlayerStorageValue(cid, storageId)
  local resultId     = db.storeQuery(string.format("SELECT `currency` FROM `players` WHERE `name` = %s ;", storageValue))

  -- Found at least 1 result
  if resultId ~= false then
    local currency = result.getDataInt(resultId, "currency")
    result.free(resultId)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, string.format("Currency: %d", currency))
  else
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Oops! No results were found!")
  end
  return true
end
Now after trying this, it is always going to "No results were found"
 
Okay so currently messing with the script trying to get it to work. im getting this error now.

Maybe to explain a little more what's happening will help. Storage Value 69693 is a string, you save this string by saying !character namehere.
So its selecting currency where the name matches the string you entered. Then im trying to grab the amount of currency from the db. its coming back weird every time i do it. i can make come back true/false/nil with different times ive tried it.


Code:
[23/03/2019 12:12:21] [Error - TalkAction Interface]
[23/03/2019 12:12:21] data/talkactions/scripts/ahnert.lua:onSay
[23/03/2019 12:12:21] Description:
[23/03/2019 12:12:21] data/talkactions/scripts/ahnert.lua:10: bad argument #2 to 'format' (number expected, got boolean)
[23/03/2019 12:12:21] stack traceback:
[23/03/2019 12:12:21]     [C]: in function 'format'
[23/03/2019 12:12:21]     data/talkactions/scripts/ahnert.lua:10: in function <data/talkactions/scripts/ahnert.lua:2>


Lua:
function onSay(cid)
local storageId = getPlayerStorageValue(cid, 69693)
local storageValue = getPlayerStorageValue(cid, storageId)
  if param ~= '' and resultId ~= true then
        local resultId = db.storeQuery("SELECT `currency` FROM `players` WHERE `name` = " .."\"".. storageId .. "\"")
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, storageId)
        local currency = result.getDataInt(resultId, "currency")
        result.free(resultId)
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, string.format("Currency: %d", currency))
  else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Oops! No results were found!")
  end
  return true
end
 
I have finally gotten it. the main issue i was having was the fact that it was case sensitive. Here is the finished code.
Lua:
function onSay(cid)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, storageId)
    local charName = getPlayerStorageValue(cid, 69693)
    local queryResult = db.storeQuery("SELECT `currency` FROM `players` WHERE `name` = " .. db.escapeString(charName) .. "COLLATE NOCASE")
    local currency = result.getDataInt(queryResult, "currency")
    result.free(queryResult)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, currency)
    return true
end
 
I have finally gotten it. the main issue i was having was the fact that it was case sensitive. Here is the finished code.
Lua:
function onSay(cid)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, storageId)
    local charName = getPlayerStorageValue(cid, 69693)
    local queryResult = db.storeQuery("SELECT `currency` FROM `players` WHERE `name` = " .. db.escapeString(charName) .. "COLLATE NOCASE")
    local currency = result.getDataInt(queryResult, "currency")
    result.free(queryResult)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, currency)
    return true
end

This is incorrect.
1. Storages were not made to store strings, only numbers. So it probably will easily bug later. It means you should get through the player guid ("id" on players table).
2. You should use string.format on query strings to avoid SQL injections. In this case it will not happen because of the db.escapeString. But this is the common practice.
3. You should verify the resultId as I showed you.
4. You should only free the result if you found anything, not give a shit and do it anyway even if no results were found.


Lua:
local storageId = 69693

function onSay(cid)
  local playerGuid = getPlayerStorageValue(cid, storageId) -- Player guid should be storage at this storage id
  local resultId   = db.storeQuery(string.format("SELECT `currency` FROM `players` WHERE `id` = %d COLLATE NOCASE", playerGuid))

  -- Found
  if resultId ~= false then -- There there is no result.next, it will get only the first result row
    local currency = result.getDataInt(resultId, "currency")
    result.free(resultId)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, string.format("Currency found: %d", currency))

  -- Not found
  else
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_BLUE, "Currency not found.")
  end
  return true
end

You didn't use ANYTHING that I showed you (the CORRECT way to code).
I gave my effort to teach you the right way, you gave a shit of what I said because you didn't even tried to do in the way I showed you.
That's why this is my last time helping you.
Good luck.
 
I mean that was highly uncalled for. You can store a string in a storage value, as i have, and i do. Its not going to bug, because it will never be saved permanently. It is only used for this one script. The guy that is helping me code literally does coding for a living, so if you think you have a problem with how the code is written, you need to take it up with him. The code works. I did try ur way, as i stated, it wasnt working, and even with him helping we couldnt get it.

Also, this is not really a script, this was only a test. I was trying to get the currency to spit out, thats it. Now that i have it, all of this is going to be put into another code.


Here is the code on how i set the storage value, so you can see there would be no chance of injection.


Lua:
function onSay(cid, words, param)
    if(param == "") then
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Please enter your void characters name by saying \"!character (name here)\".  Not sure what this means? Visit ..ip here.. to learn more.\n Just want to kill people? Ignore this message. Have fun :\)")   
        return TRUE
    end
    if playerExists(param) then
        setPlayerStorageValue(cid, 69693, param)
        local x = getPlayerStorageValue(cid, 88543)
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Success! Your character has been set to " .. param .. " for this session. When you die, or log out, remember to use !character again after logging in!")
        return TRUE
    else
        doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "Player " .. param .. " does not exist.")
        return TRUE
    end
end


Thank you for the help.
 
Last edited:
Back
Top