Jump to content

MYSQL problem with result


SweetyWolf

Recommended Posts

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?

Link to comment
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.

Link to comment

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?

Link to comment

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

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