contao/test — Issue #411: Bug in MySQL driver when using multiple databases
Author: importer
Created: 2011-11-29 05:06:31+00:00
State: open
Labels: backend, Defect, Invalid

In our project we use a template that dynamically loads information from an additional database on the same webserver as TYPOlight. To do this we developed a simple php library that is loaded from the page template.

We then got strange error messages like

```
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user '(...)'`'localhost' (using password: NO) in (...)/system/drivers/DB_Mysql.php on line 249
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in (...)/system/drivers/DB_Mysql.php on line 249
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user '(...)'`'localhost' (using password: NO) in (...)/system/drivers/DB_Mysql.php on line 249
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in (...)/system/drivers/DB_Mysql.php on line 249
Fatal error: Uncaught exception Exception with message Query error: Access denied for user '(...)'@'localhost' (using password: NO) (SELECT id, checksum FROM tl_search WHERE url=_ AND pid=_ LIMIT 0,1) thrown in (...)/system/libraries/Database.php on line 502
#000000 (...)/system/libraries/Search.php(107): Database_Statement->execute('(...)...', '51')
#1 (...)/system/modules/frontend/FrontendTemplate.php(191): Search->indexPage(Array)
#2 (...)/system/modules/frontend/PageRegular.php(109): FrontendTemplate->output()
#3 (...)/index.php(187): PageRegular->generate(Object(DB_Mysql_Result))
#4 (...)/index.php(268): Index->run()
#5 {main}
```

It seems like the database driver doesn't check if it uses the right connection, so it simply uses the first available or the last that was used.

To solve this, we changed line 249 of DB_Mysql.php (in system/drivers/) from

```
return "'" . mysql_real_escape_string($strString) . "'";
```

to

```
return "'" . mysql_real_escape_string($strString, $this->resConnection) . "'";
```

so TYPOlight is basically forced to use its own database connection. Now everything works fine for us.

--- _Originally created by anonymous on January 13th, 2009, at 06:27am (ID 411)_