• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

[SQL/Lua] Getting Value from Column

Otfan125

Well-Known Member
Joined
Mar 1, 2008
Messages
169
Solutions
1
Reaction score
55
Location
Thais
Hey guys, I'm trying to write a script that gets executed and sets a condition on a player based on some SQL entry in the 'players' table... I'm using TFS 1.3 if thats useful.

What I'm trying to do is create a onLogin script that reads the database and checks if a player has a specific value in a specific column in 'players'... or, for example:

Lua:
function onLogin(player)
    local res = db.storeQuery("SELECT `blademaster` FROM `players` WHERE `name` = `Angel` ;")
    print(res)
    return true
end

here, 'blademaster' is the column in each player's table that I'm trying to retrieve, though I get the error:

Code:
Angel has logged in.
[Error - mysql_real_query] Query: SELECT `blademaster` FROM `players` WHERE `players` = `Angel` ;
Message: Unknown column 'players' in 'where clause'
[Error - mysql_store_result] Query: SELECT `blademaster` FROM `players` WHERE `players` = `Angel` ;
Message: Unknown column 'players' in 'where clause'
false

or

Code:
Angel has logged in.
[Error - mysql_real_query] Query: SELECT `blademaster` FROM `players` WHERE `name` = `Angel` ;
Message: Unknown column 'Angel' in 'where clause'
[Error - mysql_store_result] Query: SELECT `blademaster` FROM `players` WHERE `name` = `Angel` ;
Message: Unknown column 'Angel' in 'where clause'
false
Any pointers? Whats the proper SQL synthax? Thanks
 
Solution
Use quotating marks " when looking for strings in SQL queries

SQL:
SELECT `blademaster` FROM `players` WHERE `name` = "Angel";
Use quotating marks " when looking for strings in SQL queries

SQL:
SELECT `blademaster` FROM `players` WHERE `name` = "Angel";
 
Solution
Thanks @Alpha :]
Here is the code to make it work in Lua, just add a ' \ ' behind every quotation inside quotations

Lua:
function onLogin(player)
    local res = db.storeQuery("SELECT `blademaster` FROM `players` WHERE `name` = \"Angel\";")
    print(res)
    return true
end
 
Thanks @Alpha :]
Here is the code to make it work in Lua, just add a ' \ ' behind every quotation inside quotations

Lua:
function onLogin(player)
    local res = db.storeQuery("SELECT `blademaster` FROM `players` WHERE `name` = \"Angel\";")
    print(res)
    return true
end
2 more variants that also work:

Lua:
function onLogin(player)
    local res = db.storeQuery(("SELECT `blademaster` FROM `players` WHERE `name` = %s;"):format("Angel"))
    print(res)
    return true
end


Lua:
function onLogin(player)
    local res = db.storeQuery("SELECT `blademaster` FROM `players` WHERE `name` = 'Angel';")
    print(res)
    return true
end
 
Back
Top