How do I update a second database without changing the active connection? - drupal

I am using Drupal 9.2.10 and would like to update a database entry that is not in the default database.
when using select and join i use:
$query = \Drupal::database()->select('default.table1', 'table1');
$query->leftJoin('db_name2.table2', 'table2', 'table2.id=table1.id);
which works fine
however,
when trying to update a row in the second db by \Drupal::database()->update('db_name2.table2') Drupal will add the prefix of the default db so that is will actually try to update default.db_name2.table2 which will fail
I could set the active db by \Drupal\Core\Database\Database::setActiveConnection('db_name2');
however would like to avoid it, and use the db name instead
I tried the following with no luck:
\Drupal::database()->update('`db_name2.table2`')`
\Drupal::database()->update('"db_name2.table2"')`
\Drupal::database()->update('{db_name2.table2}')`
Can you help?

In Drupal\Core\Database\Query\Update __toString(), Drupal forces adding the active table by wrapping the table name with curly brackets:
$query = $comments . 'UPDATE {' . $this->connection->escapeTable($this->table) . '} SET ' . implode(', ', $update_fields);
In Drupal\Core\Database\Query\Select toString(), Drupal recognizes the possibility of the table name being a complte db_name.table_name by building the query as follows:
if (strpos($table_string, '.') === FALSE) {
$table_string = '{' . $table_string . '}';
}
Not sure why it's different.
*** Edit ***
Please refer to issue at Drupal site:
https://www.drupal.org/project/drupal/issues/3253479#comment-14334898

Related

Error 1366 sent MariaDb version 10.1.48 on wrong strings

In PHP, i reproduce the problem with this command as an example, introducing some junk chars in the data i want to store :
$my_val = 'blabla' . chr(0xC2) . chr(0x94) . 'blabla';
$query = "UPDATE my_table SET my_field='" . $my_val . "' WHERE id='1'";
my_database, my_table, and my_field are ALL set to utf8mb4 and this cannot be changed.
I would like MariaDb to accept the wrong chars instead of returning systematic 1366 (Incorrect string value: '\xC2\x94b ' on this query.)
This problem is new on this MariaDb version : everything was ok on previous version.
I tried to add the IGNORE command to my query, but it ignore the error without writing the data.
Thank you for your help !

Sqlite update with inner query select

According to all examples my query should be executing. I am trying to update new column on my table with the last 4 digits of the phone number like so :
UPDATE users
SET users.phone_last_4 = t.lastFour
FROM
(
select substr( phone_number, -4) as lastFour from users
) t;
Also tried this:
UPDATE users
SET users.phone_last_4 = t.lastFour
FROM
(
select substr( phone_number, -4) as lastFour from users
) AS t;
Both fail with same error :
near ".": syntax error: UPDATE users
SET users.
What could I possibly do wrong here?
SQLite does not support joins for the UPDATE statement and also this syntax containing FROM .
In your case I can't see why you need it.
Just do:
UPDATE users
SET phone_last_4 = substr(phone_number, -4)

wpdb select table no arguments

I am using new wpdb for accessing database with wordpress.
$mydb = new wpdb($username,$password,$database,$hostname);
$sql = $mydb->prepare("SELECT * FROM " . $table);
$results = $mydb->get_results($sql);
This line produces error:
$sql = $mydb->prepare("SELECT * FROM " . $table);
wpdb::prepare was called incorrectly. wpdb::prepare() requires at least two arguments.
All below statements produce same error:
$sql = $mydb->prepare("SELECT * FROM $table");
$sql = $mydb->prepare("SELECT * FROM '%s'", $table);
How do I write it without arguments?
(I am using wordpress 3.5)
Take the single quotes off from '%s' in your last example and that should work assuming $table is a valid table name.
Edit: $wpdb->prepare() works similar to php's sprintf() on a more limited scale. If you just pass it a single parameter, it doesn't do anything. See Andrew Nacin's post on this after 3.5 was released: http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/
The second parameter will replace the first instance of a replaceable character set, i.e. %s, %d or %f, and third parameter will replace the second instance, and so on.

MODx Revo Gallery: How to set tags by choosing from list and filter albums by them in resourses

I need to get albums from gallery for 'Closes' which has categoties 'dress, skirts...' and collections (each element from subcategory can relate to different collections) and I need get closes elements filtering by categories, collections, and other parameters. But it's difficult to input the same tags in each Gallery item when creating it. How can I make such a catalog with Gallery?
And even more – each gallery item (element) have multiple images of the same item, so now i create Albums -> Subalbums -> Items.
I'm not sure can I do this with Gallery extra? May be use MIGx somehow... and code Gallery.
Im not sure entirely what your after but I would be tempted to create my own PHP snippet that searches the ModX database its self. Eg:
//TEMP VAR SEARCH
$sql = 'SELECT * FROM `modx_site_tmplvar_contentvalues` WHERE `value` LIKE "%WHAT EVER TAGS YOU HAVE%"';
$tvqresult = mysql_query($sql);
$num_rowstvq = mysql_num_rows($tvqresult);
while ($rowtvq = mysql_fetch_array($tvqresult)) {
$contid = $rowtvq['contentid'];
//MAIN QUERY
$mainsql = 'SELECT * FROM `modx_site_content` WHERE `id` = ' . $rowtvq['contentid'] . ' AND `template` = 12 AND `published` = 1 AND `deleted` = 0';
$resultmain = mysql_query($mainsql);
while ($row = mysql_fetch_array($resultmain)) {
echo "[[Ditto? &parents=`134` &documents=" . $row['id'] . " &tpl=`tempchunk`]]";
}//END MAIN LOOP
}
NOTE: This is just normal SQL - with Revo I believe you will had to make some slight adjustments to have it work with PDO.

Why doesn't this WordPress $wpdb query work?

I am doing the following:
$type = 'attachment';
$images = $wpdb->get_results($wpdb->prepare('
SELECT p.*
FROM wp_%d_posts p
WHERE p.post_parent =%d
AND p.post_type = "%s"
', $blog_id, $page->ID, $type),OBJECT);
var_dump($images);
If I remove the line 'AND p.post_type = "%s"' then I get results returned, otherwise I get an empty array returned. If I run the query direct against the DB in a mysql client, I get results.
There is no error, just an empty result set. I am doing similar queries throughout my file and they are working so I'm not looking for "don't do it like that" style replies. I just need to understand why this isn't working and fix it.
PHP 5.3, MYSQL 5.1. WordPress MU 2.9.2
Do not Quote "%s". From the WordPress site, "Notice that you do not have to worry about quoting strings. Instead of passing the variables directly into the SQL query, use a %s placeholder for strings and a %d placedolder for integers."
Example:
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );

Resources