Jump to content

Need help with mysql table


itoko

Recommended Posts

Hi,

i have a problem with a panel script, by mysql database.

The script is connected to the database, but there are no table in the database, so nothng can be saved, or loaded.

The default table is 'userdata', but in the database, there are no table created and im not a very good scripter, so i ask your help.

is there the mysql part of the script:

-- Database connection info 
sqlHostname = "***.**.**.***" 
sqlUsername = "*********" 
sqlPassword = "*******" 
sqlDefaultDatabase = "*********" 
sqlDefaultTable = "userdata" 
  
  
  
  
  
  
  
-- Connect to the database 
function mySQLConnect() 
    sqlConnection = mysql_connect(sqlHostname,sqlUsername,sqlPassword,sqlDefaultDatabase) 
end 
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),mySQLConnect) 
  
  
  
  
-- Make a new entry to the mySQL database 
function checkSqlData(source) 
    if (sqlConnection == nil) then 
        mySQLConnect() 
    end 
    local player = source 
    local serial = getPlayerSerial(player) 
    local entryCheck = sqlConnection:query("SELECT serial FROM "..sqlDefaultTable.." WHERE serial='"..serial.."'") 
    if (entryCheck) then 
        local result = mysql_result(entryCheck,1,1) 
        if (result == "") or (result == nil) then 
            outputDebugString("New entry added to the mySQL database!") 
            sqlConnection:query("INSERT INTO "..sqlDefaultTable.." (serial) VALUES ('"..serial.."')") 
            sqlConnection:query("UPDATE "..sqlDefaultTable.." SET playerName='"..string.gsub(getPlayerName(player),"#%x%x%x%x%x%x", "").."' WHERE serial='"..serial.."'") 
        end 
    else 
        outputDebugString("New entry added to the mySQL database!") 
        sqlConnection:query("INSERT INTO "..sqlDefaultTable.." (serial) VALUES ('"..serial.."')") 
        sqlConnection:query("UPDATE "..sqlDefaultTable.." SET playerName='"..string.gsub(getPlayerName(player),"#%x%x%x%x%x%x", "").."' WHERE serial='"..serial.."'") 
    end 
end 
addEventHandler("onPlayerJoin",getRootElement(),checkSqlData) 
  
  
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()), 
function() 
    for i,player in ipairs (getElementsByType("player")) do 
        checkSqlData(player) 
    end 
end) 
  
  
  
  
  
  
-- Load data from the database 
function loadPlayerData (player,datatype) 
    if (sqlConnection == nil) then 
        restartResource(getThisResource()) 
    end 
    if (player) and (datatype) then 
        local serial = getPlayerSerial(player) 
        local findQuery = sqlConnection:query("SELECT "..datatype.." FROM "..sqlDefaultTable.." WHERE serial='"..serial.."'") 
        if (findQuery) then 
            local result = mysql_result(findQuery,1,1) 
            if not (result == nil) then 
                if not (result == "") then 
                    mysql_free_result(findQuery) 
                    return result 
                else 
                    mysql_free_result(findQuery) 
                    return 0 
                end 
            else 
                return 0 
            end 
        else 
            --outputDebugString("Failed to get "..datatype.." for player "..getPlayerName(player).." @ findQuery") 
            --outputDebugString("mysql_query failed: (" .. mysql_errno(sqlConnection) .. ") " .. mysql_error(sqlConnection)) 
        end 
    end 
end 
  
  
  
  
  
-- Save data to the database 
function savePlayerData (player,datatype,newvalue) 
    if (sqlConnection == nil) then 
        restartResource(getThisResource()) 
    end 
    if (player) and (datatype) and (newvalue) then 
        local serial = getPlayerSerial(player) 
        local saveData = sqlConnection:query("UPDATE "..sqlDefaultTable.." SET "..datatype.."='"..newvalue.."' WHERE serial='"..serial.."'") 
        if (saveData == nil) then 
            outputDebugString("Error executing the save query: (" .. mysql_errno(sqlConnection) .. ") " .. mysql_error(sqlConnection)) 
        end 
    end 
end 
  

i hope someone can help me.

Regards, Itoko

Link to comment

they are no table, that the problem. and sorry i forgotten the error

they are bad arguments #1 on all tonumber: value expected (to change the money, stats ect) and when i try change my personal details:

error executing the save query: (line) table 'username.userdata' doesn't exist (i mean the sqlUsername)

Link to comment

ok thx the table is created, but i have the same problem with all tonumber, its dont work, and the error table doesn't exist is now:

error executing the save query: (1054) unkown column 'serial' in 'where cause'

sry but i really dont know the sql

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