I have been trying to create a PHP script that will periodically move "completed" rows from a table on my Joomla site to a different table. The query I wrote works just fine in PHPMyAdmin:
INSERT INTO my_calsgovdocs.sent_copy
SELECT * FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%';
DELETE FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%';
I attempted to translate it into some PHP code which could run inside Joomla, and I've pasted that code below. It returns an "Unexpected T_STRING" error which points to the line below which starts ->insert into
, and it has now occurred to me that the script wouldn't work because "insert into" isn't a valid method name! So far I can't find an equivalent method to be used inside Joomla. This was my attempt at the code:
try
{
$db->transactionStart();
$query = $db->getQuery(true);
$query
->insert into($db->quoteName('sent_copy'))
->select('*')
->from($db->quoteName('entered_copy'))
->where($db->quoteName('Status') . ' LIKE ' . $db->quote('%Sent%') . ';')
->delete from($db->quoteName('entered_copy'))
->where($db->quoteName('Status') . ' LIKE ' . $db->quote('%Sent%'));
$db->setQuery($query);
$result = $db->execute();
$db->transactionCommit();
}
catch (Exception $e)
{
$db->transactionRollback();
JErrorPage::render($e);
}
Anyone have an idea how I can accomplish this inside Joomla? I'd prefer (as you may have noticed above) to do it in one transaction so that, if there's an error, I won't have a mess on my hands.
You could try doing it in plain old php? Something like
$conf = JFactory::getConfig(); // load your config
try{
$link = mysqli_connect($conf->get('host'), $conf->get('user'),
$conf->get('password'), $conf->get('db'));
mysqli_begin_transaction($link, MYSQLI_TRANS_START_READ_WRITE);
mysqli_query($link, "INSERT INTO my_calsgovdocs.sent_copy
SELECT * FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%'");
mysqli_query($link, "DELETE FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%'");
mysqli_commit($link);
}
catch (Exception $e)
{
mysqli_rollback($link);
JErrorPage::render($e);
}
mysqli_close($link);
$db->setQuery allows being passed a query string as an argument instead of an object. See "preparing the query": https://docs.joomla.org/J1.5:Accessing_the_database_using_JDatabase
I've also suggested running two of these queries as part of the same transaction.
I unfortunately don't have a joomla installation handy to test this, please comment if you find it doesn't work.
try
{
$db->transactionStart();
$query = $db->getQuery(true);
$query1 = "INSERT INTO my_calsgovdocs.sent_copy
SELECT * FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%'";
$db->setQuery($query1);
$result1 = $db->execute();
$query2 = "DELETE FROM my_calsgovdocs.entered_copy
WHERE `Status` LIKE '%Sent%'";
$db->setQuery($query2);
$result2 = $db->execute();
$db->transactionCommit();
}
catch (Exception $e)
{
$db->transactionRollback();
JErrorPage::render($e);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.