Jump to content

I need help


Hero192

Recommended Posts

Hey guys, im getting a vehicle color ( r, g, b) to storing it to a database but it save just R others no, i mean g and b

Here is my code, can anyone give me a hand?

function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) 
  if not doesVehExists(vehicleName) then    
        local color =  r..","..g..","..b   
        dbExec(connection, "INSERT INTO vehicle VALUES ( ?,?,?,? )", tostring(vehicleName),tostring(vehicleid),tostring(vehicleType), tostring(color)) 
      return true 
 else  
      return false 
   end 
end 

And i want to get the veh color later so please tell me the solution of this

Edited by Guest
Link to comment

I don't know the first problem but maybe i can help you about getting vehicle color into db. You must edit dbQuery function cuz i dont know your table(s) name. Maybe it wont work (i wrote that on my cellphone)

  
function getVehicleColorFromDb(vehicleName) 
    local query = dbQuery( connection, "SELECT color FROM `vehicle` WHERE vehicleName='".. tostring(vehicleName) .."'") 
    local result, numrows, errmsg = dbPoll( query, -1 ) 
    if (result) then 
        local vehicleColorString = tostring(result[1]["color"]) 
    end 
    local vehicleColorTable = split(vehicleColorString, string.byte(',')) 
    if vehicleColorTable then 
    return tonumber(vehicleColorTable[1]), tonumber(vehicleColorTable[2]), tonumber(vehicleColorTable[3]) -- r, g, b 
    end 
return false 
end 
  

Link to comment
function doesVehExists(vehicleName) 
    local qh = dbQuery(connection, "SELECT * FROM `vehicle` WHERE `vehicleName`=? LIMIT 1", vehicleName) 
    local result = dbPoll(qh, -1) 
    if (result and #result > 0) then 
        return true 
    end 
    return false 
end 
  
function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) 
    if (not doesVehExists(vehicleName)) then   
        local color =  toJSON({r, g, b}) 
        dbExec(connection, "INSERT INTO `vehicle` VALUES (?, ?, ?, ?)", tostring(vehicleName), tostring(vehicleid), tostring(vehicleType), color) 
        return true 
    end 
    return false 
end 
  
function getVehicleColor(vehicleid) 
    local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) 
    local result = dbPoll(qh, -1) 
    if (result and #result > 0) then 
        return tonumber(unpack(fromJSON(result[1].color))) 
    end 
    return false 
end 

I ended up using JSON to store the colour, because there are multiple values and it's better than slabbing each colour in a separate column. I also don't know your column names, so make sure to replace them in the code.

Link to comment

Yes good way but it give error bad argument #1 to 'unpack' (table expected, got nil)

in this part

function getVehiclesColor(vehicleid) 
    local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) 
    local result = dbPoll(qh, -1) 
    if (result and #result > 0) then 
        return tonumber(unpack(fromJSON(result[1].color))) 
    end 
    return false 
end 

I do like that to get the vehicle color but not working

local r,g,b =  getVehiclesColor(vehicleid) -- i define the vehicle id 
setVehicleColor(veh,r,g,b) 

Link to comment

Then you need to update it in the database with the new JSON string. As TAPL said, remove the 'tonumber' next to 'unpack'. That was my bad there.

Your code should end up looking like this.

function doesVehExists(vehicleName) 
    local qh = dbQuery(connection, "SELECT * FROM `vehicle` WHERE `vehicleName`=? LIMIT 1", vehicleName) 
    local result = dbPoll(qh, -1) 
    if (result and #result > 0) then 
        return true 
    end 
    return false 
end 
  
function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) 
    if (not doesVehExists(vehicleName)) then   
        local color =  toJSON({r, g, b}) 
        dbExec(connection, "INSERT INTO `vehicle` VALUES (?, ?, ?, ?)", tostring(vehicleName), tostring(vehicleid), tostring(vehicleType), color) 
        return true 
    end 
    return false 
end 
  
function getVehicleColor(vehicleid) 
    local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) 
    local result = dbPoll(qh, -1) 
    if (result and #result > 0) then 
        return unpack(fromJSON(result[1].color)) 
    end 
    return false 
end 

Update the database with the JSON string and get back to us.

Link to comment
local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) 
return unpack(fromJSON(result[1].color)) 

Replace 'color' (and possibly `vehicleid` in the SQL query itself) with the name of the corresponding column.

Link to comment
local qh = dbQuery(connection, "SELECT `color` FROM `vehicle` WHERE `vehicleid`=? LIMIT 1", vehicleid) 
return unpack(fromJSON(result[1].color)) 

Replace 'color' (and possibly `vehicleid` in the SQL query itself) with the name of the corresponding column.

Show me example please, i won't be wrong

Link to comment

It's working, i sent the r,g,b from client to server side and i tested it by using outputChatBox function the problem is when i use

toJSON 

to store the r,g,b colors in one columns at the database of mysql it store just r but g and b no so when i try to return the color it return false so here where is the problem exist

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