Jump to content

I need help


Hero192

Recommended Posts

Posted (edited)

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
Posted

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 
  

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

Posted

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) 

Posted

Have you updated your database with JSON string or it still the old string? Also you shouldn't use tonumber within unpack.

Posted
Have you updated your database with JSON string or it still the old string? Also you shouldn't use tonumber within unpack.

I didn't update anything, can you fix that with example

Posted

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.

Posted

But when i try to get r,g,b from the function it tell me the same error

error bad argument #1 to 'unpack' (table expected, got nil)

local r,g,b =  getVehiclesColor(vehicleid) 

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

Posted
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

Posted

No, I gave already given you the code. What you need to do is fill in the bits I have told you to. I don't know the setup of what you're working with, only you do. All you need to do is replace some words here and there.

Posted

Yes i know but my problem is toJSON don't save all the numbers it save only r but g, b no i checked the mysql table in the color column and i saw that

Posted
Yes i know but my problem is toJSON don't save all the numbers it save only r but g, b no i checked the mysql table in the color column and i saw that

Anyone can help me to solve this?

Posted

Try to output r, g and b to the chat, those in this function:

function addToDatabase(vehicleName, vehicleid, vehicleType, r, g, b) 

Posted

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

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