lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This is the source I use.

It uses a luasql mysql connection in mysql.con.

   function mysql_query(query,connection)
       if (connection==nil) then connection=mysql.con end
       return connection:execute(query)
   end
function mysql_get(query,connection)
       local rows={}
       local cur = assert (mysql_query(query,connection))
       local row = cur:fetch ({}, "a")
       while (row) do
           local row2={}
           for key,value in pairs(row)  do
               row2[key]=value
           end
           table.insert(rows,row2)
           row = cur:fetch (row, "a")
       end
       return rows
   end

Since there are 3 elements in the database, (which are correctly printed if I add a print_r(row) right after the while beginning.

But if I replace
       while (row) do
           local row2={}
           for key,value in pairs(row)  do
               row2[key]=value
           end
           table.insert(rows,row2)
           row = cur:fetch (row, "a")
       end

with:
       while (row) do
           table.insert(rows,row)
           row = cur:fetch (row, "a")
       end

it won't work and raises errors like I described before.

Kind regards,
 Jan (DracoBlue)

David Given schrieb:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jan Schütze wrote:
[...]
The following pseudo code won't add every 3 rows to the table rows,
because row is a variable so rows would contain one item, and 3 pointers
to it.
            rows={}
       local row = fetchRow()
       while (row) do
           table.insert(rows,row)
           row = fetchRow()
       end

The only way you'd end up with the same item three times is if fetchRow()
were somehow returning the same item every time. It's not reusing the same
table to return each item, is it --- can we see the implementation?

- --
┌── dg@cowlark.com ─── http://www.cowlark.com ───────────────────
│ "There is nothing in the world so dangerous --- and I mean *nothing* ---
│ as a children's story that happens to be true." --- Master Li Kao, _The
│ Bridge of Birds_
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with MultiZilla - http://enigmail.mozdev.org

iD8DBQFFu3Rnf9E0noFvlzgRAqwyAKCcqhtJ33uE2rgKWcZ0pWN3cnygDACfeJ2/
ArP4T0P73WTQeQ7/65KfEmU=
=5ego
-----END PGP SIGNATURE-----