Jump to content

How to Display Mysql result on client side


SuperM

Recommended Posts

Hello there I have the following script:

--SERVER SIDE FILE
if getElementModel(source) == 1000 then
	ID = getElementID(source)
end

local result = mysql:query("SELECT id, x, y, z, rotation, dimension, interior FROM table WHERE id=".. ID .."")
local rowID = mysql:fetch_assoc(result)
local ID = tonumber(rowID["id"])
local X = tonumber(rowID["x"])
local Y = tonumber(rowID["y"])
local Z = tonumber(rowID["z"])
local Rotation = tonumber(rowID["rotation"])
local Dimension = tonumber(rowID["dimension"])
local Interior = tonumber(rowID["interior"])


--CLIENT SIDE FILE
function Draw()
	local object = getElementsByType("object")
	
	for k,element in ipairs(object) do
		if getElementModel(element) == 1000 then
			ID = getElementID(element)
			dxDrawTextOnElement (element, ID, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
		end
	end
end
addEventHandler ("onClientRender", getRootElement(), Draw)

But I need more information at the moment I can get the ID client side, but how do I can get the other results from SQL on there to be shown like:

--CLIENT SIDE FILE
...
			dxDrawTextOnElement (element, ID, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, X, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, Y, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, Z, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, ROTATION, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, DIMENSION, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
			dxDrawTextOnElement (element, INTERIOR, 0.75, _, _, _, _, _, 2, _, tocolor(0,0,0,255))
...

 

Link to comment
-- SERVER
local query = dbQuery(connection 'SELECT `id`, `x`, `y`, `z`, `rotation`, `dimension`, `interior` FROM `table` WHERE `ID` = ?', ID);
local result = dbPoll(query, -1);

triggerClientEvent(source, 'mysqlResultsData', result);

dbFree(query);

-- CLIENT

addEvent('mysqlResultsData', true)
addEventHandler('mysqlResultsData', resourceRoot, function()
	-- etc
end )

 

Link to comment
On 25/10/2019 at 06:50, XaskeL said:

-- SERVER
local query = dbQuery(connection 'SELECT `id`, `x`, `y`, `z`, `rotation`, `dimension`, `interior` FROM `table` WHERE `ID` = ?', ID);
local result = dbPoll(query, -1);

triggerClientEvent(source, 'mysqlResultsData', result);

dbFree(query);

-- CLIENT

addEvent('mysqlResultsData', true)
addEventHandler('mysqlResultsData', resourceRoot, function()
	-- etc
end )

 

I have done the following:

 

--SERVER
local result1 = mysql:query("SELECT id, x, y, z, rotation, dimension, interior FROM table")
local rowID1 = mysql:fetch_assoc(result1)
			
local ID1 = tonumber(rowID1["id"])
local X1 = tonumber(rowID1["x"])
local Y1 = tonumber(rowID1["y"])
local Z1 = tonumber(rowID1["z"])
local Rotation1 = tonumber(rowID1["rotation"])
local Dimension1 = tonumber(rowID1["dimension"])
local Interior1 = tonumber(rowID1["interior"])
triggerClientEvent(source, 'ResultsDataB', result1);

--Client
addEvent('ResultsDataB', true)
addEventHandler('ResultsDataB', resourceRoot, function()
	outputDebugString(ID1)
	outputDebugString(X1)
	outputDebugString(Y1)
	outputDebugString(Z1)
	outputDebugString(Rotation1)
	outputDebugString(Dimension1)
	outputDebugString(Interior1)
end )

But I'm not getting anything just this Warning: 

Bad argument @ 'triggerClientEvent' [Expected element at argument 3, got number '71']

 

 

With your code it does not work gives me errors in local result = dbPoll(query, -1); 

Also with something like this (not sure if it should even work)

	local query1 = mysql:query( 'SELECT id, x, y, z, rotation, dimension, interior FROM table');
local result2 = mysql:dbPoll(query1, -1);

triggerClientEvent(source, 'mysqlResultsData', result2);

It gives the following error 

call: failed to call 'mysql:dbPoll' [string "?"]
Bad argument @ 'triggerClientEvent' [Expected element at argument 3, got boolean]

 

Link to comment
17 hours ago, DNL291 said:

source must be a reference to the player element. Show your current code here please.

At the moment I have the following code:
 

--SERVER

function ttttt(source)
	local result1 = mysql:query("SELECT id, x, y, z, rotation, dimension, interior FROM test WHERE id=1")
			local rowID1 = mysql:fetch_assoc(result1)
			
			local ID1 = tonumber(rowID1["id"])
			local X1 = tonumber(rowID1["x"])
			local Y1 = tonumber(rowID1["y"])
			local Z1 = tonumber(rowID1["z"])
			local Rotation1 = tonumber(rowID1["rotation"])
			local Dimension1 = tonumber(rowID1["dimension"])
			local Interior1 = tonumber(rowID1["interior"])
triggerClientEvent(source, 'ResultsDataB', source, result1);
end
addCommandHandler("tscript", ttttt, false, false)

--CLIENT
addEvent('ResultsDataB', true)
addEventHandler('ResultsDataB', resourceRoot, function()
	outputDebugString(ID1)
	outputDebugString(X1)
	outputDebugString(Y1)
	outputDebugString(Z1)
	outputDebugString(Rotation1)
	outputDebugString(Dimension1)
	outputDebugString(Interior1)
	local object = getElementsByType("object")

	for k,element in ipairs(object) do
		if getElementModel(element) == 1319 then
		
		ID = getElementID(element)
			dxDrawTextOnElement (element, Dimension1, 1.3, _, _, _, _, _, 3, _, tocolor(0,0,0,255))
		end
	end
end )

And at the moment it does not give any error and does nothing when I do /tscript

Link to comment
  • Moderators

Wrong base element provided.

 

See:

addEventHandler('ResultsDataB', resourceRoot, function()

This event will only trigger when the resourceRoot is provided or a (+n indirect) child of the resourceRoot.

A player is not a child or an indirect child of the resourceRoot. See: (players blue and resourceRoot purple)

Tre.png

 

 

 

So to solve that issue.

triggerClientEvent(source, 'ResultsDataB', resourceRoot, result1);
Edited by IIYAMA
Link to comment
2 minutes ago, IIYAMA said:

Wrong base element provided.

 

See:


addEventHandler('ResultsDataB', resourceRoot, function()

This even will only trigger when the resourceRoot is provided or a (+n indirect) child of the resourceRoot.

A player is not a child or an indirect child of the resourceRoot. See: (players blue and resourceRoot purple)

Tre.png

 

 

 

So to solve that issue.


triggerClientEvent(source, 'ResultsDataB', resourceRoot, result1);

Alright, with that line of code, I'm getting just a single outputDebugString and it says "INFO: nil"

Link to comment
  • Moderators
15 minutes ago, SuperM said:

Alright, with that line of code, I'm getting just a single outputDebugString and it says "INFO: nil"

triggerClientEvent(source, 'ResultsDataB', resourceRoot, rowID1 );

And debug with iprint.

addEventHandler('ResultsDataB', resourceRoot, function(rowID1)
iprint(rowID1)

 

Edited by IIYAMA
Link to comment
  • Moderators
43 minutes ago, SuperM said:

Still getting just one outputDebugString with "INFO: nil"
And my query is workig, this is what I get from SQL when I run it: 2EpKggspQCC-VmtpU50B0A.png

 

 

You are 'thinking' that the variables are being send over, which is not the case. You can only send the data, which can gets stored in to new variables.

 

 

Data goes in here:

triggerClientEvent(source, 'ResultsDataB', resourceRoot, IN1, IN2, IN3);

 

and come out here:

addEventHandler('ResultsDataB', resourceRoot, function(OUT1, OUT2, OUT3)

 

By using the same variable names, you can indeed pretend that you did send them over.

 

 

 

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