Mysql Fetch with while

Hello ! I am exposing my problem, I would like to make an SQL query to display the name of the companies that owns

entreprise_status = 1

the problem, is that my SQL query only returns me the first result, did not find any solution I turn to the forum

Sorry for my English (Google translate life lmao)

RegisterServerEvent('entreprise_jobs_annonce:public_entreprise')
AddEventHandler('entreprise_jobs_annonce:public_entreprise', function()
    MySQL.Async.fetchAll("SELECT custome_name FROM entreprise WHERE entreprise_status = @entreprise_status", {['@entreprise_status'] = 1}, function(result)
        if result ~= nil then
            print(result[1].custome_name)
        end
    end)
end)

have you tried
print(result[2].custome_name)

1 Like

Unfortunately this does not work, the change returns the 2nd name of the company present in my table and not the whole of the porky company name entreprise_status = 1

by using result[1] you are only returning the first result. You will need to run through a loop to go through each result individually.

1 Like

How would I do this loop? :thinking:

for k,v in pairs(result) do
    print(result[v].custome_name)
end

Maybe? I could be very wrong. Haha

1 Like

Rha: / unfortunately this does work, but it allows me to move forward in my search thank you!

If you want to see the error returned => http://prntscr.com/iorkq5

Problem solved ! Thank you very much ! this would save me more time

Final code .

RegisterServerEvent('entreprise_jobs_annonce:public_entreprise')
AddEventHandler('entreprise_jobs_annonce:public_entreprise', function()
    MySQL.Async.fetchAll("SELECT * FROM entreprise WHERE entreprise_status = @entreprise_status ", { ['@entreprise_status'] = 1 }, function(result)
        if result ~= nil then
            for k,v in pairs(result) do
                print(result[k].custome_name)
            end
        end
    end)
end)

my bad, I did v instead of k. Haha

1 Like