Can't use variable in mysql dashboard variables

I’ve setup a variable $network in my dashboard, which returns backbone and hertzien. I would like to get every IP of the router of each network with another variable $routerIP depending of the first one.
I’ve tried to build the variable like this:

SELECT DISTINCT(agent_host) FROM bandwidth WHERE network = $network;
-- And tried
SELECT DISTINCT(agent_host) FROM bandwidth WHERE network IN ( '$network' );
-- With and without quotes for both

But those queries returns nothing.
This query works:

SELECT DISTINCT(agent_host) FROM bandwidth WHERE network IN ('backbone', 'hertzien');

But using this method, I get all the addresses of both networks without being able to separate the two networks on my dashboard.

When you are editing your panel, if you click on Query Inspector and then look at the Query you can see exactly how your query is being translated and exactly what SQL is being sent to the database. And therefore should be able to see what is wrong etc.

I use my multi-value variables like this:
...WHERE network IN ($network)
No quotes as the variable should be a single value in quotes or a multi value with each value in quotes separated by commas.

As you said you tried no quotes so you need to look at query inspector to see the translated query and what is wrong to get an empty result.

1 Like

It seems like you’re trying to use a variable $network to dynamically filter the results of your SQL query. However, SQL does not directly support variables in the same way that scripting languages do. Instead, you would typically need to use parameterized queries or dynamic SQL generation depending on the database system you’re using.

Assuming you’re using a database system that supports parameterized queries (e.g., MySQL, PostgreSQL, etc.), you would need to use prepared statements or bind variables to achieve this dynamically.

If your database system does not support prepared statements or bind variables, you may need to resort to dynamically generating the SQL query string in your application code before executing it against the database.

Remember to ensure proper handling of user input to prevent SQL injection vulnerabilities when using dynamic SQL generation.

If you’re still having trouble, please provide more details about the database system you’re using, and I can provide more specific guidance.

Well @codlord, thank you very much. I don’t understand why but it works now with the solution you suggested :grinning:. Maybe a problem while refreshing the dashboard…