Use API Parameters in Custom SQL Query Endpoint
-
Hi,
Could i know if we can use Custom Parameters in Custom SQL Query Endpoint?
-
Cause I received no reply, I Managed to add Parameters by the following snippet, Hope it will benefit somebody searching :
add_filter( ‘rest_routes_custom_sql_query’, ‘ss’ ,99,3);
function ss($query,$data,$endpoint) {
$url = (isset($_SERVER[‘HTTPS’]) && $_SERVER[‘HTTPS’] === ‘on’ ? “https” : “http”) . “://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]”;
$url_components = parse_url($url);
parse_str($url_components[‘query’], $params);
$query = str_replace(“<Parameter1>”, $params[‘Parameter1’], $query);
$query = str_replace(“<Parameter2>”, $params[‘Parameter2’], $query);return $query;
}`I am writing a simple Custom MySQL Query, as follows:
UPDATE zaqgutmy_WPUKX.NK_MKTSTATUS SET code = $signal WHERE instrument = $symbol
I want the plugin to replace the 2 variables started with $, by the actual Custom Parameters…. But it is not being accepted… What would be the expected format to write this snipped to replace the Custom Parameter by its value coming from the http request ?I have also attempted the Custom SQL Query with the following Sintax :
UPDATE zaqgutmy_WPUKX.NK_MKTSTATUS SET code = <Parameter1> WHERE instrument = “<Parameter2>”
But the plugin responds with this:{
“success”: false,
“data”: “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘<Parameter1> WHERE instrument = “<Parameter2>”’ at line 1”
}
So, it is not clear how to tell the plugin to replace the variables in the MySQL query, with data received from the API that the plugin is creating… would you mind clarifying the syntax required by the plugin, please…. I couldn’t find it anywhere in the plugin documentation.

please see the previous image
i think you should replace the dollar sign, because its not allowed in the URL, instead, you can use <parameter>, for both api parameter and sql parameter
if you wonder where to write the previous code,
This is a code snippet, it should be added to Functions.php file, or better, to be added as a snippet in a plugin called “Code Snippets“,
Hello,
We are working to support URL parameter placeholders in SQL queries by default, it should be available very soon. For now, I suggest using @esamzenhom approach with custom code.
Good news! We just added this feature and one more really cool: https://restroutes.com/rest-routes-5-5-0/
Please let us know what you think about it.
Thats amazing ! I will try the new features !
Thanks both of you for the messages !
The topic ‘Use API Parameters in Custom SQL Query Endpoint’ is closed to new replies.