• 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!

Lua Return number of rows from select in lua

Sacarus

Member
Joined
Mar 15, 2016
Messages
52
Reaction score
7
Hi, sorry for the dumb question, but I just cant get the number of rows returned by a select. I tried using while result.next(data) and it loops just two times, executing the same code in sql returns 4 rows.
The following code prints "false". Does anyone know how to get that COUNT(*) ?? Any other way to get the total of rows returned gonna work, just tell me if you know some.

Lua:
local data= db.query("SELECT COUNT(*) FROM(SELECT * FROM `player_coins` WHERE `player_id` = 1) x")
  if data~= false then
    local lines= result.getDataInt(data, "COUNT(*)")
    print(lines)
  end
Thank you!
 
Solution
Lua:
local res = db.storeQuery("SELECT COUNT(*) FROM `player_coins` WHERE `player_id` = 1")
print(result.getNumber(res, "COUNT(*)"))

use db.storeQuery when you want to get values from the result, db.query just returns true/false based on success of execution
try


Lua:
local data= db.query("SELECT count(*) FROM `player_coins` WHERE `player_id` = 1")
  if data then
    print(data)
  end
 
Lua:
    local data = db.query("SELECT COUNT(*) FROM `player_coins` WHERE `player_id` = 1;")
    if data then
        print(data:getDataInt('data'))
    end
 
Lua:
local res = db.storeQuery("SELECT COUNT(*) FROM `player_coins` WHERE `player_id` = 1")
print(result.getNumber(res, "COUNT(*)"))

use db.storeQuery when you want to get values from the result, db.query just returns true/false based on success of execution
 
Solution
TFS 1.x

Lua:
    local count = 0
    local resultId = db.storeQuery("SELECT COUNT(*) AS `count` FROM `player_coins` WHERE `player_id` = 1;")
    if resultId ~= false then
        count = data:getDataInt('count')
        result.free(resultId)
    end
    print(count)
 
Last edited:
Sorry for the delay. I tried all your suggestions and always was printing "false", like there was not an int value to get from 'count', even with storeQuery.
But, I found some example in playerdeath that worked:

Lua:
  local resultId = db.storeQuery("SELECT `player_id` FROM `player_coins` WHERE `player_id` = 1")

  local rows = 0
  local tmpResultId = resultId
  while tmpResultId ~= false do
    tmpResultId = result.next(resultId)
    rows = rows + 1
    print(rows)
  end

Thank you all, guys
 
User Static_ told you the solution with storeQuery function. Your new script is using the same. His post should be marked as answer.
 
No problem, I changed. Anyway, even with storeQuery, it was printing "false", I tried all the codes people gave me here, using storeQuery instead of query, I only got "false" like there was not Int value in result. It just worked using storeQuery in a "while" with result.next(query). I dont know why, but, didn't work trying to get the select's value (count). I needed to count the rows in lua script, not in sql. Thank you static_told.
 
Back
Top