Jump to content

mysql result


tosfera

Recommended Posts

Hey guys,

I created a function that will accept a query and returns the mysql result, which works good. Now I want to store the mysql result into a variable like so;

local result = exports[""]:( ); 

So, whenever I debug inside my . Its returning a mysql result, which is great! But whenever I debug the result, it just gives me back the query and not the result... anyone has an idea how the hell that can happen?xD

code

local query; 
query = exports["Main-system"]:clean_query ( "SELECT `AccountId`, `AccountPassword`, `AccountUsername` FROM `Accounts` WHERE `AccountPassword` = '".. mysql_escape_string ( db, value1 ) .."' AND `AccountUsername` = '".. mysql_escape_string ( db, value2 ) .."'" ); 

Link to comment

Btw, its still not the thing I was searching for. I'm getting an error that mysql_num_rows needs a mysqlResult. Which my function returns 100%. but whenever I store the returned value in a variable it returns the query instead of the mysqlResult.

entire command;

addCommandHandler("log", 
    function ( thePlayer, command, value1, value2 ) 
        local query; 
        if ( value1 and value2 ) then 
            query = exports["Main-system"]:clean_query ( "SELECT `AccountId`, `AccountPassword`, `AccountUsername` FROM `Accounts` WHERE `AccountPassword` = '".. mysql_escape_string ( db, value1 ) .."' AND `AccountUsername` = '".. mysql_escape_string ( db, value2 ) .."'" ); 
        elseif ( value1 and not value2 ) then 
            query = exports["Main-system"]:clean_query ( "SELECT `AccountId`, `AccountPassword`, `AccountUsername` FROM `Accounts` WHERE `AccountPassword` = '".. value1 .."' and `AccountUsername` = '".. getPlayerName ( thePlayer ) .."'" ); 
        else 
            outputChatBox ( "Wrong usage: /log [] ", thePlayer ); 
            return; 
        end 
        if ( query ) then 
            if ( mysql_num_rows ( query ) > 0 ) then 
                local row = mysql_fetch_assoc ( result ); 
                setElementData ( thePlayer, "loggedin", true ); 
                setElementData ( thePlayer, "accountid", row["AccountId"] ); 
                spawnPlayer ( source, 1686, -2334, 14 ); 
                fadeCamera ( source, true ); 
                setCameraTarget ( source, source ); 
            else 
                outputChatBox ( "nopes", thePlayer ); 
                return; 
            end 
        end 
    end 
); 

Link to comment

Have you tried

  
-- Assuming you've already defined handler. 
local query = mysql_query(handler, "SELECT version()") 
local result = {} 
while true do 
    local row = mysql_fetch_assoc(query) 
    if (not row) then break end 
    table.insert(result, row) 
end 
return result 

Edited by Guest
Link to comment
Somehow my mysqlHandler is empty in another file, this isn't wrong is it .-. :

file1;

function getMysqlConnection () 
    return db; 
end 

file2

local db = exports["Main-system"]:getMysqlConnection(); 

Errm, could you send me your whole database library?

(Mask the username,pass,host and so on)

Link to comment
pmed it. :)

I guess the last thing I PM'd you should fix it.

    addCommandHandler("log", 
        function ( thePlayer, command, value1, value2 ) 
            local query; 
            if ( value1 and value2 ) then 
                query = exports["Main-system"]:clean_query ( "SELECT `AccountId`, `AccountPassword`, `AccountUsername` FROM `Accounts` WHERE `AccountPassword` = '".. mysql_escape_string ( db, value1 ) .."' AND `AccountUsername` = '".. mysql_escape_string ( db, value2 ) .."'" ); 
            elseif ( value1 and not value2 ) then 
                query = exports["Main-system"]:clean_query ( "SELECT `AccountId`, `AccountPassword`, `AccountUsername` FROM `Accounts` WHERE `AccountPassword` = '".. value1 .."' and `AccountUsername` = '".. getPlayerName ( thePlayer ) .."'" ); 
            else 
                outputChatBox ( "Wrong usage: /log [] ", thePlayer ); 
                return; 
            end 
            if ( query > 0) then 
                    local row = query[1] 
                    setElementData ( thePlayer, "loggedin", true ); 
                    setElementData ( thePlayer, "accountid", row["AccountId"] ); 
                    spawnPlayer ( source, 1686, -2334, 14 ); 
                    fadeCamera ( source, true ); 
                    setCameraTarget ( source, source ); 
             else 
                    outputChatBox ( "nopes", thePlayer ); 
                    return; 
            end 
        end 
    ); 

Link to comment

Nope, you're returning a table, and mysql_num_rows needs a mysql_Result haha. but with your edit in the log command it should work, sec.

edit; it works, thanks.

Can you also tell me, why doesn't this one work;

local db = exports["Main-system"]:getMysqlConnection(); 

outputDebugString gives me; getMysqlConnect();

Link to comment
Nope, you're returning a table, and mysql_num_rows needs a mysql_Result haha. but with your edit in the log command it should work, sec.

edit; it works, thanks.

Can you also tell me, why doesn't this one work;

local db = exports["Main-system"]:getMysqlConnection(); 

outputDebugString gives me; getMysqlConnect();

I have no idea haha!

Anyways, glad I was able to help you :)

BTW, my fix will save you 3-4 lines on each query. ^^

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