Jump to content

mysql not returning a value


TheNightRider

Recommended Posts

Hi guys am working on a function for a server which uses mysql, Am constructing a script to manage the database.

I came across an issue when am going to query the database for users who have admin level of >=6 its meant to display the GUI to the user and if they don't its meant to trigger a client function informing the user. However it isn't seem to be working. There are no errors nor bad arguments in the debugger and am stumped.

  
  
mysql = exports.mysql 
function showmenu()--check user has rights or not. 
  
     
    local result = mysql:query("SELECT admin FROM 'accounts' WHERE username")  
    if (mysql:num_rows(result)>=6) then 
    triggerClientEvent(source,"ShowMenuC",true)--show user interface. 
    outputChatBox('I did something YAY XD',playerSource);-- test if working or not 
        else 
  
  
    triggerClientEvent(source,"notify",true)--tell user. 
    outputChatBox('Sorry No Can Do!!!',playerSource);-- test if working or not 
    end 
end 
addCommandHandler("dbman",showmenu) 
  

Thanks

Link to comment

then suppose we want to use the command /db CompleteNameofAdmin

mysql = exports.mysql 
function showmenu(source, cmd, person)--check user has rights or not. 
  
   if not person then return end 
    local result = mysql:query("SELECT admin FROM 'accounts' WHERE username == '"..person.."'") 
    if (mysql:num_rows(result)>=6) then 
    triggerClientEvent(source,"ShowMenuC",true)--show user interface. 
    outputChatBox('I did something YAY XD',playerSource);-- test if working or not 
        else 
  
  
    triggerClientEvent(source,"notify",true)--tell user. 
    outputChatBox('Sorry No Can Do!!!',playerSource);-- test if working or not 
    end 
end 
addCommandHandler("dbman",showmenu) 

If JohnPepper is level 6

/dbman JohnPepper will show you it.

If you write /dbman John it will show you there's no admin.

You must add FULL NAME since it's not getPlayerFromPartName but from Name.

Link to comment

What i understood is that you have a column in your table which is where the level is defined.

mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local result = mysql:query_fetch_assoc ( "SELECT admin FROM `accounts` WHERE `username`='".. getPlayerName ( playerSource ) .."'" ) 
    if ( result and tonumber ( result [ "admin" ] ) >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

Link to comment
What i understood is that you have a column in your table which is where the level is defined.
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`='".. getPlayerName ( playerSource ) .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

I get an error saying line 6 attempt to return a nil value its regarding the line after the query.

Link to comment

I have tried so many ways to try and solve the issue but unsuccessful I know it is communicating with the database but isn't seem to be doing has expected. Could someone please explain why its wrong?

  
  
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`='".. getPlayerName ( playerSource ) .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "result.admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 
  
  

Link to comment

I am used to dbExec. I will quote you an example though.

--- DB VARIABLES --- 
host = "localhost" -- host goes here 
username = "root" -- db username 
password = "pa$$word" -- db password 
dbname = "iLoveTrains" -- db name 
-------------------- 
db = dbConnect( "mysql", "dbname="..dbname..";host="..host, username, password) 
  
function showmenu ( playerSource ) 
     local pName = getPlayerName ( playerSource ) 
     local consulta = dbQuery(db,"SELECT admin FROM `accounts` WHERE nick = '"..pName.."'") 
     local Result = dbPoll ( consulta, -1 ) 
     if (Result[1] == nil) then return end -- Shit happened :3 
     if (Result[1].admin >= 6) -- epic WIN 
          triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
          outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
     else 
          triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
          outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
     end 
end 
  
addCommandHandler ( "dbman", showmenu ) 

Link to comment
I am used to dbExec. I will quote you an example though.
--- DB VARIABLES --- 
host = "localhost" -- host goes here 
username = "root" -- db username 
password = "pa$$word" -- db password 
dbname = "iLoveTrains" -- db name 
-------------------- 
db = dbConnect( "mysql", "dbname="..dbname..";host="..host, username, password) 
  
function showmenu ( playerSource ) 
     local pName = getPlayerName ( playerSource ) 
     local consulta = dbQuery(db,"SELECT admin FROM `accounts` WHERE nick = '"..pName.."'") 
     local Result = dbPoll ( consulta, -1 ) 
     if (Result[1] == nil) then return end -- :~ happened :3 
     if (Result[1].admin >= 6) -- epic WIN 
          triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
          outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
     else 
          triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
          outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
     end 
end 
  
addCommandHandler ( "dbman", showmenu ) 

Thanks mate but am using a mysql script to do the connection etc.

Link to comment
Well, you can maybe try this :P

It does not do nothing but when I added a test to show on chat what result is getting it seems to be counting in HEX LOL.

I see its just returning a nil causing it to end but however it shouldn't be right?

  
  
  
db = dbConnect( "mysql", "dbname="..dbname..";host="..host, username, password,port) 
function showmenu ( playerSource ) 
     local pName = getPlayerName ( playerSource ) 
     local consulta = dbQuery(db,"SELECT admin FROM `accounts` WHERE username = '"..pName.."'") 
     local Result = dbPoll ( consulta, -1 ) 
     outputChatBox ( tostring ( Result ) ) 
     if (Result[1] == nil) then return end -- :~ happened :3 
     if (Result[1].admin >= 6) then -- epic WIN 
          triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
          outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
     else 
          triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
          outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
     end 
end 
  

Link to comment
Line 7: replace with

local consulta = dbQuery(db,"SELECT admin FROM `accounts` WHERE username == '"..pName.."'")

Yeah what about it and what is the point in having more than 1 script which has connection details?

PS that duplicates the Primary Key, do not need to do connection because the query itself is querying the database. 1 Way we tried we got an error saying unknown Column Username etc meaning it was saying the Username it got from the table was a Column so it was in away getting confused but like I said it shows the script is querying the database xD

Link to comment

Then use this instead

mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local pName = getPlayerName ( playerSource ) 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`=='".. pName .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "result.admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

Link to comment
Then use this instead
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local pName = getPlayerName ( playerSource ) 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`=='".. pName .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "result.admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

having username`=='".. pName .."'" returns an syntax error due to the == if its only a = i says attempt to index a nil value regarding the following line

Link to comment
Then use this instead
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local pName = getPlayerName ( playerSource ) 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`=='".. pName .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "result.admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

having username`=='".. pName .."'" returns an syntax error due to the == if its only a = i says attempt to index a nil value regarding the following line

Yea yeah, I dont know why I thought it should have been double ==. Yet, it should work, it's extremely weird. The one I posted BEFORE worked.

Link to comment
Then use this instead
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local pName = getPlayerName ( playerSource ) 
    local result = mysql:query ( "SELECT admin FROM `accounts` WHERE `username`=='".. pName .."'" ) 
    if ( result and mysql:fetch_assoc ( result ) [ "result.admin" ] >= 6 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 

having username`=='".. pName .."'" returns an syntax error due to the == if its only a = i says attempt to index a nil value regarding the following line

Yea yeah, I dont know why I thought it should have been double ==. Yet, it should work, it's extremely weird. The one I posted BEFORE worked.

it does not work :wink:

Link to comment

Its working thanks to SoildSnake but there is something wrong with the client triggers keeps telling me not added clientside but they are.

  
  
mysql = exports.mysql 
function showmenu ( playerSource )--check user has rights or not. 
    local result = mysql:query_fetch_assoc ( "SELECT admin FROM `accounts` WHERE `username`='".. getPlayerName ( playerSource ) .."'" ) 
    if ( result and tonumber ( result [ "admin" ] ) >= 1 ) then -- Check if the level is 6 or higher. 
        triggerClientEvent ( playerSource, "ShowMenuC", playerSource, true )--show user interface. 
        outputChatBox ( 'I did something YAY XD', playerSource );-- test if working or not 
    else 
        triggerClientEvent ( playerSource, "notify", playerSource, true )--tell user. 
        outputChatBox ( 'Sorry No Can Do!!!', playerSource );-- test if working or not 
    end 
end 
addCommandHandler ( "dbman", showmenu ) 
  
  

EDIT: Sorry forget to post the clientside function

  
  
addEvent("tellusr",true) 
function notify(playerSource)--inform low level user. 
  
  
outputChatBox('You do not have permission to access this terminal',playerSource);-- notify user. 
end 
addEventHandler ("telluser",getRootElement(),notify) 
  
  

Edited by Guest
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...