Jump to content

Fetching MySQL rows


Hale

Recommended Posts

Hi, to cut the story short, I'm learning MySQL functions within Lua and I want to know how to fetch multiple rows, for instance: I need to find a row that has stored '0' in its column 'fuel', but there are more rows containing such data in the same column. So, what does this return?

local fuel = mysql:query_fetch_assoc("SELECT name FROM `vehicles` WHERE `owner` = " .. mysql:escape_string(characterID) .. " and `fuel` = 0 and `type` = 1") 

In this case "name" is the vehicle name, and there is a possibility of more rows containing health = 0 and type = 1, and I'm wondering how to fetch more rows to get the "name" from them and then I'd output the name into their chatbox, something like

Your following vehicles have no fuel left: 
Sultan 
Infernus 
. 
. 

PS: This is just an example.

Thanks in advance, if I didn't make myself clear somewhere please say so and I'll explain it as simply as possible!

Link to comment

Firstly, I would recommend using the db functions.

When you perform a query and get the result, you end up with a table. Each row is identified with a numerical index, like this:

result = { 
    [1] = {column = data}, 
    [2] = {column = data}, 
} 

If the result of your SQL query has more than 1 row, you will end up with more than 1 entry in your resultant table (returned from dbPoll). If you want to list that data like you said in the OP, just loop through it.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...