Jump to content

MYSQL problem with result


SweetyWolf

Recommended Posts

Posted

Hello everyone. I've got a problem with mysql. After several hours of my server working, I getting problem "bad argument #1 to 'mysql_fetch_assoc' (mysqlResult expected, got nil)"

Code:

  
local result = mysql_query ( database ,"SELECT * FROM `accounts` WHERE `name` = '"..getPlayerName(source).."' LIMIT 1") 
local row = mysql_fetch_assoc(result) 
  

Can anyone help me?

Posted
Try checking first if result is not nil (or false)
if result then 
row = mysql_fetch_assoc(result) 
end 

That won't solve the problem, that will just make mysql_fetch_assoc be called if the query was sucessful.

Posted
Try checking first if result is not nil (or false)
if result then 
row = mysql_fetch_assoc(result) 
end 

That won't solve the problem, that will just make mysql_fetch_assoc be called if the query was sucessful.

True, but this was the problem, wasn't this?

Posted
Try checking first if result is not nil (or false)
if result then 
row = mysql_fetch_assoc(result) 
end 

That won't solve the problem, that will just make mysql_fetch_assoc be called if the query was sucessful.

True, but this was the problem, wasn't this?

Well, I guess he wants to know why is him getting that problem, not how to "by-pass" it.

Posted

His problem was, that he gets an error, with this he won't get it ;)

But i don't know why is he getting the error, maybe there is no record for the given player, or because the query is not ready yet.

Posted (edited)
Yes, I need an advice about why I getting this problem.

There is a record for this player.

try this to get, what is the error:

  outputChatBox("Error: "..mysql_error(database).." Error code: "..mysql errno(database))  

Edited by Guest
Posted

The error is "mysql_query failed: (2006) MySQL server has gone away". As I mentioned this error means, that connection with database failed. So, I changed code:

  
result = mysql_query ( database ,"SELECT * FROM `accounts` WHERE `name` = '"..getPlayerName(source).."' LIMIT 1") 
    if not result then  
        if mysql_errno(database) == 2006 then 
            outputDebugString("mysql_query failed: (" .. mysql_errno(database) .. ") " .. mysql_error(database)) 
            mysql_close ( database ) 
            database = mysql_connect(...) 
            result = mysql_query ( database ,"SELECT * FROM `accounts` WHERE `name` = '"..getPlayerName(source).."' LIMIT 1") 
        end 
    end 
  

This code is good?

Posted

yes, but you may want to consider opening the connection every time before a block of queries and closing it when you're done, together with the freeing of the result (as opposed to creating the sql connection just on resource start for example)

mysql just isn't made for persistent connections

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...