Okay
  Public Ticket #1319609
using Functions in query to pass variable to view
Closed

Comments

  •  1
    Richard Aarnink started the conversation

    Hi,  I use complex queries, so after updating to wpDataTables 2.0 I had to create some views in MySQL. To prevent having to create many views I created a view which uses 2 variables in its SELECT statement. For this I used an example I found on stackoverflow.com 

    The example function:

    create function p1() returns INTEGER DETERMINISTIC NO SQL return @p1;

    and the view:

    create view h_parm as
    select * from sw_hardware_big where unit_id = p1() ;

    Then you can call a view with a parameter:

    select s.* from (select @p1:=12 p) parm , h_parm s;

    I tried this in phpMySQL and I got my view working, also I created a working query passing 2 variables to the view. 

    Next step I tried to copy the query to the wpDataTables (create a Table) where it looks like this:

    SELECT gm.meting as "meting", gm.datum as "datum" FROM 
    (select @aquaID:=2 a) ap, 
    (select @pinID:=11 p) pp, 
    week_gemiddelden_per_dag gm
    ORDER BY gm.datum
    

    However, when I  press the save button in wpDatatables I get an error message:

    Error

    cannot calculate position of @aquaID within @aquaID:=2 a) ap, (select @pinID:=11 p) pp, week_gemiddelden_per_dag gm ORDER BY gm.datum

    Is there any way to use functions in wpDataTables?

  •  472
    Isidora replied

    Hi Richard,
    Thank you for your purchase/

    Our recommendation when creating mysql tables is not to use functions like SUM, GROUP BY, ORDER BY, LIMIT , JOIN etc. You have a complicated query and my suggestion is to make a  just a VIEW in phpmyadmin without any functions like 

    SELECT * FROM view_name

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables

  •  1
    Richard Aarnink replied

    Hi Milan,

    thats the thing, i have created a view in mySQL. but to prevent me from having to create a whole bunch of views, I would like to pass a variable to the view. 

    SELECT * FROM [view_name]  is not enough if you want to pass a variable to the view.

    This is where my question comes in, i did manage to find a working solution using functions in mySQL, however this does not seem to work in wpDataTables. 

    Is there any way? 



  •  472
    Isidora replied

    Hi Richard,

    Sorry to disappoint you but unfortunately variables in mysql queries are not supported.
    But you can use placeholders.
    You can take a look at our documentation about placeholders here.

    Kind Regards, 

    Isidora Markovic

    wpDataTables: FAQFacebookTwitterFront-end and back-end demoDocs

    Amelia: FAQFacebookTwitter |  Amelia demo sites | Docs

    You can try our wpDataTables add-ons before purchase on these sandbox sites:

    Powerful FiltersGravity Forms Integration for wpDataTablesFormidable Forms Integration for wpDataTables