• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

[Solved] Execute multiple queries in a PHP cron plugin

georgie

Member
Hello

I would run many "INSERT" queries in a scheduled task, separated by a semi-colon.

I well recover my data, I well create queries (copying-pasting queries generated in DB = OK).

But PHP does not seem to want to execute them.

I tried 2 ways to create my sequence of queries, first this "classical" way (which work for me when I have just one query):

Code:
$query = "
BEGIN ;
INSERT INTO customer (civility, lastname, firstname, email)
VALUES ('$A_var', '$Another_Var', '$Another_Var', '$Another_Var') ;
INSERT INTO address (id_customer, country)
VALUES (LAST_INSERT_ID(), '$Another_Var') ;
INSERT INTO telephone (id_customer, id_address, code_tel1, tel1)
VALUES ( (SELECT MAX(id_customer) FROM customer) , (SELECT MAX(id_address) from address) , '$Another_Var', '$Another_Var') ;
INSERT INTO customer_organisme (id_customer, id_organisme)
VALUES ( (SELECT MAX(id_customer) FROM customer) , 'A_Value') ;
COMMIT ;
" ;
$db->setQuery($query);
$db->query();
It does not work.

After documenting, it seems this other method is recommended, for multiple queries:
Code:
$query  = "BEGIN ; ";
$query .= "INSERT INTO customer (civility, lastname, firstname, email)
VALUES ('$A_var', '$Another_Var', '$Another_Var', '$Another_Var') ; ";
$query .= "INSERT INTO address (id_customer, country)
VALUES (LAST_INSERT_ID(), '$Another_Var') ; ";
$query .= "INSERT INTO telephone (id_customer, id_address, code_tel1, tel1)
VALUES ( (SELECT MAX(id_customer) FROM customer) , (SELECT MAX(id_address) from address) , '$Another_Var', '$Another_Var') ; ";
$query .= "INSERT INTO customer_organisme (id_customer, id_organisme)
VALUES ( (SELECT MAX(id_customer) FROM customer) , 'A_Value') ; ";
$query .= "COMMIT ;";

OK the sequence generated is the same and works in DB (with a var_dump).
But I can not execute them in my PHP cron plugin. I tried many syntaxs, playing with this in various form:
Code:
$db->multi_query($query) ;
$db->query() ;

No way...:(

I have seen this PHP doc, I tried, but without success ("if...do...if...while... all with my "$db"):
http://php.net/manual/en/mysqli.multi-query.php

Please can you help me?:p
 
Please note my sequence of queries is correct with a var_dump, now I think I just need to "send" it in DB, to execute.
Do you think I really need to split up?
 
Var_dump is only showing the string you have composed, it can be anything.
It doesn't mean that it is some valid query.
And even if it's a valid combination to run directly in the DB you can't run it through Joomla's JDatabase.
 
OK thanks.

My problem is (was) I have (had) to insert in a remote DB, in tables linked, without InnoDB. So I think I can not split my sequence of queries (please what do you think about it?).

I am still working, but I think problem is solved : with Joomla/Fabrik/PHP cron, I do just one insert query in remote DB, in a special table created only for this. Then a trigger in this remote DB dispatchs in the required tables.

THX for infos:

#Fab
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top