Lua语言MySQL:我的文件错误,ipairs

时间:2017-03-26 04:04:32

标签: mysql mysqli lua lua-table

我的LUA脚本中有一个problam,我正在尝试在MTA游戏服务器上创建一个新服务器,并在我的脚本中使用LUA Langauge,但我有一个控制台错误,不知道如何修复它< / p>

Consle错误

File.lua 117 Line Error和文件代码

--The main function to open the entire advertisements system.
function openAdvertisements( player, command ) local advertisements = { } --These will hold our advertisements to send to the client and populate our advertisement tables.
if not player then player = source end

--Fetch all of the advertisements from the database

for _, ad in ipairs( exports.mysql:select('advertisements') ) do
    if tonumber( ad.expiry ) >= tonumber( getRealTime().timestamp ) then --Check if the advertisement has expired, delete it if so.
        ad.author = exports.mysql:select_one( "characters", { id = ad.created_by } ).charactername
        table.insert( advertisements, ad )
    else
        deleteAdvertisement( ad.id )
    end
end

triggerClientEvent( player, resourceName .. ":display_all", root, `advertisements, exports.integration:isPlayerAdmin( player ) ) --Send the` `advertisements to the client to create the GUI.`

第117行错误: for _,ad in ipairs(exports.mysql:select('advertisements'))do

1 个答案:

答案 0 :(得分:0)

试试这个。

database = dbConnect( "mysql", "dbname=databasename;host=1.2.3.4;charset=utf8", "username", "password" )
function getData()
    local result = {}
    local query = dbQuery(database, "select * from advertisements")
    result = dbPoll(query,-1)
    dbFree(query)
if #result > 0 then
        return result
    else    
        return {}
    end
end

--The main function to open the entire advertisements system.
function openAdvertisements( player, command ) local advertisements = { } --These will hold our advertisements to send to the client and populate our advertisement tables.
if not player then player = source end

--Fetch all of the advertisements from the database

for _, ad in ipairs( getData() ) do
    if tonumber( ad.expiry ) >= tonumber( getRealTime().timestamp ) then --Check if the advertisement has expired, delete it if so.
        ad.author = exports.mysql:select_one( "characters", { id = ad.created_by } ).charactername
        table.insert( advertisements, ad )
    else
        deleteAdvertisement( ad.id )
    end
end

triggerClientEvent( player, resourceName .. ":display_all", root, `advertisements, exports.integration:isPlayerAdmin( player ) ) --Send the` `advertisements to the client to create the GUI.`