I am trying to get the data from another database in Drupal 7. But I am not able to get the data. Here is the code what I have tried.
I have added another database information in settings.php
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'drupal_testing',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'prefix' => '',
);
$databases['sakshi']['default'] = array(
'driver' => 'mysql',
'database' => 'test',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'prefix' => '',
);
And added the code in page.tpl.php for testing the connection.
<div class="col-lg-3 col-md-3 col-sm-12 col-xs-12 padtop30">
<h4> DB Connection test </h4>
<?php
try{
echo "inside try block";
db_set_active('sakshi');
$results = db_query("select name from test.user_names where phone = 432323");
$records = $results->fetchAll();
foreach ($records as $record) {
echo $record;
}
}catch(\PDOException $ex){
echo "inside catch block";
echo $ex;
}finally{
echo "finally block is executed";
db_set_active('default');
}
?>
</div>
Now when i am loading the data (refreshing the page) i am getting the error like
'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.semaphore' doesn't exist' in E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc:2227 Stack trace: #0 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2227): PDOStatement->execute(Array) #1 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(697): DatabaseStatementBase->execute(Array, Array) #2 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\database\database.inc(2406): DatabaseConnection->query('SELECT expire, ...', Array, Array) #3 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(167): db_query('SELECT expire, ...', Array) #4 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\lock.inc(146): lock_may_be_available('rules_get_cache...') #5 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(368): lock_acquire('rules_get_cache...', 60) #6 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\rules.module(1026): rules_get_cache('event_watchdog') #7 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\modules\rules\modules\events.inc(180): rules_invoke_event('watchdog', Array) #8 [internal function]: rules_watchdog(Array) #9 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\module.inc(926): call_user_func_array('rules_watchdog', Array) #10 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(1997): module_invoke('rules', 'watchdog', Array) #11 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(210): watchdog('php', '%type: !message...', Array, 3) #12 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\errors.inc(75): _drupal_log_error(Array, true) #13 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\bootstrap.inc(2576): _drupal_error_handler_real(4096, 'Object of class...', 'E:\\xampp5.6.20\\...', 321, Array) #14 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\sites\all\themes\my_theme\page.tpl.php(321): _drupal_error_handler(4096, 'Object of class...', 'E:\\xampp5.6.20\\...', 321, Array) #15 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1526): include('E:\\xampp5.6.20\\...') #16 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\theme.inc(1208): theme_render_template('sites/all/theme...', Array) #17 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(6045): theme('page', Array) #18 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(5907): drupal_render(Array) #19 E:\xampp5.6.20\htdocs\drupal\drupal-7.53\includes\common.inc(2748): drupal_render_page('
I am new to Drupal. Can any one help me in this regard.
While according to the documentation, there is nothing wrong with the way you have set up your second connection, I have never been able to get it to work like that. The only way I have had it working is with the below, although I would agree there doesn't seem to be anything fundamentally different:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'drupal_db',
'username' => 'root',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'second_database' =>
array (
'default' =>
array (
'driver' => 'mysql',
'database' => 'second_db',
'username' => 'root',
'password' => 'password',
'host' => 'localhost',
'port' => '',
'prefix' => '',
),
),
);
Then you should be able to do db_set_active('second_database').
The "Base table or view not found: 1146 Table 'test.semaphore' doesn't exist'" message is usually when Drupal has tried to do something Drupally while still attached to your second database, so don't forget to do db_set_active() to return the connection back to the Drupal database. There is no need to add 'default' as default is the assumed default database.
You should do this directly after fetching the data you need. So in your example, it should come after $records = $results->fetchAll();
Or, if you only need this connection in one place/ infrequently, you could set it up on the fly. More info here
Hope this helps
Related
new to laravel framework, I am running multiple database connections for a project I am working on. Just a simple test against one of the mysql instance,
<p>{{ $data2 = DB::connection('mysql2')->table('test')->get() }}</p>
gives a simple dump but when run simmilar test against the ms sql server 2012, it errors out
<p>{{ $data3 = DB::connection('sqlsrv')->table('test1')->get() }}</p>
The error is:
ErrorException in helpers.php line 519:
Method Illuminate\Support\Collection::__toString() must return a string value
my db config is similar to other examples I have seen posted online.
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', 'SQL3.company.ca'),
'database' => env('DB_DATABASE', 'test'),
'username' => env('DB_USERNAME', 'test'),
'password' => env('DB_PASSWORD', '#######'),
'collation' => 'SQL_Latin1_General_CP1_CI_AS',
'charset' => 'latin1',
'prefix' => '',
],
Did I set up something wrong? any direction I should be looking at?
I want to use my email on my server with CakePHP IMAP Custom Datasource.
In database.php I have:-
public $myCustomEmail = array(
'datasource' => 'ImapSource',
'server' => 'test.com',
'username' => 'info#test.com',
'password' => 'email password',
'port' => 143,
'ssl' => true,
'encoding' => 'UTF-8',
'error_handler' => 'php',
'auto_mark_as' => array(
'Seen',
// 'Answered',
// 'Flagged',
// 'Deleted',
// 'Draft',
),
);
When I set port to 143 or ssl to true I get this error:-
Error: Unable to get imap_thread after 4 retries. 'TLS/SSL failure for radindesign.com: SSL negotiation failed'
When ssl is set to false or I change the port I get this error:-
Unable to get imap_thread after 4 retries. 'Certificate failure for test.com: self signed certificate: /CN=linux10.centraldnserver.com/emailAddress=ssl#linux10.centraldnserver.com'
What's wrong with the IMAP authentication?
CakePHP IMAP Custom Datasource didnt help me i use from this :
// Configure your imap mailboxes
$mailboxes = array(
array(
'label' => 'Gmail',
'mailbox' => '{imap.gmail.com:993/imap/ssl}INBOX',
'username' => 'yourusername#gmail.com',
'password' => 'yourpassword'
),
array(
'label' => 'My Cpanel website',
'mailbox' => '{mail.yourdomain.com:143/notls}INBOX',
'username' => 'info+yourdomain.com',
'password' => 'yourpassword'
),
array(
'label' => 'Another mail account',
'mailbox' => '{mail.yourdomain.com:143/notls}INBOX',
'username' => 'info#yourdomain.com',
'password' => 'yourpassword'
)
);
//check inbox
$current_mailbox = array(
'label' => 'My Cpanel website',
'mailbox' => '{mail.test.com:143/notls}INBOX',
'username' => 'info#test.com',
'password' => '***'
);
// Open an IMAP stream to our mailbox
$stream = #imap_open($current_mailbox['mailbox'], $current_mailbox['username'], $current_mailbox['password']);
$emails = imap_search($stream, 'SINCE ' . date('d-M-Y', strtotime("-1 week")));
// If we've got some email IDs, sort them from new to old and show them
rsort($emails);
$i = 0;
$overview = array();
foreach ($emails as $email_id) {
// Fetch the email's overview and show subject, from and date.
$overview[$i] = imap_fetch_overview($stream, $email_id, 0);
$i++;
}
var_dump($overview);
I have an odd error in a CakePHP 3 Application I am developing and I can't figure what the issue is.The application and issue is as follows:
I have a Wishlist
That Wishlist can have many Wishlist Items
I am using Angular/Ajax to make a call and delete a WishlistItem
http://domain.com/wishlist_items/delete/219
This does not work and it returns the following error:
array_merge(): Argument #2 is not an array
/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php 54
The WishlistItemsController.php in question is:
public function delete($id = null) {
$this->autoRender = false;
$this->request->allowMethod(['post', 'delete']);
$wishlistItem = $this->WishlistItems->get($id);
if ($this->WishlistItems->delete($wishlistItem)) {
echo json_encode(['message' => ['type' => 'success', 'text' => 'This wishlist item has been deleted successfully']]);
}
else {
echo json_encode(['message' => ['type' => 'error', 'text' => 'This wishlist item could not be deleted']]);
}
return;
}
The Wishlist Items have no dependents and the error is coming from this file http://api.cakephp.org/3.0/source-class-Cake.ORM.Association.DependentDeleteTrait.html#19-57 line 54
My table files look like this:
WishlistsTable.php
$this->hasMany('WishlistItems', [
'foreignKey' => 'wishlist_id',
'dependent' => true,
'cascadeCallbacks' => true,
]);
==================================================
WishlistItemsTable.php
$this->addBehavior('Timestamp');
$this->addBehavior('Ratings.Ratable');
$this->belongsTo('Wishlists', [
'foreignKey' => 'wishlist_id'
]);
$this->addBehavior('CounterCache', [
'Wishlists' => ['items_count']
]);
I cannot understand why cascadeDelete function is being used when this Table does not have any dependents.
STACKSTRACE:
Whoops\Exception\ErrorException thrown with message "array_merge(): Argument #2 is not an array"
Stacktrace:
#15 Whoops\Exception\ErrorException in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php:54
#14 Whoops\Run:handleError in /var/www/domain.com/dev/website/vendor/gourmet/whoops/src/Error/WhoopsHandler.php:27
#13 Gourmet\Whoops\Error\WhoopsHandler:_displayError in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Error/BaseErrorHandler.php:139
#12 Cake\Error\BaseErrorHandler:handleError in <#unknown>:0
#11 array_merge in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Association/DependentDeleteTrait.php:54
#10 Cake\ORM\Association\HasMany:cascadeDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/AssociationCollection.php:273
#9 Cake\ORM\AssociationCollection:cascadeDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1705
#8 Cake\ORM\Table:_processDelete in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1643
#7 Cake\ORM\Table:Cake\ORM\{closure} in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Database/Connection.php:557
#6 Cake\Database\Connection:transactional in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/ORM/Table.php:1648
#5 Cake\ORM\Table:delete in /var/www/domain.com/dev/website/src/Controller/WishlistItemsController.php:139
#4 App\Controller\WishlistItemsController:delete in <#unknown>:0
#3 call_user_func_array in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Controller/Controller.php:429
#2 Cake\Controller\Controller:invokeAction in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Routing/Dispatcher.php:114
#1 Cake\Routing\Dispatcher:_invoke in /var/www/domain.com/dev/website/vendor/cakephp/cakephp/src/Routing/Dispatcher.php:87
#0 Cake\Routing\Dispatcher:dispatch in /var/www/domain.com/dev/website/webroot/index.php:37
If there were really only BelongsTo associations on the table object where you are calling delete(), then such a control flow should never happen, so there need to be more associations defined from elsewhere.
Given that the Ratings.Ratable behavior belongs to dereuromark/cakephp-ratings, I'd suspect that the problem are the conditions that are being defined for the HasMany association that the behavior adds to the table:
https://github.com/dereuromark/cakephp-ratings/.../Behavior/RatableBehavior.php#L85
$this->_table->hasMany('Ratings', [
'className' => $this->_config['rateClass'],
'foreignKey' => $this->_config['foreignKey'],
'unique' => true,
'conditions' => '', // <<<<<< There it is
'fields' => '',
'dependent' => true,
//'table' => 'sandbox_ratings'
]
);
That value ends up being passed to array_merge() as the second argument in DependentDeleteTrait::cascadeDelete(), hence the error.
So, that's a bug in the plugin, and I'd suggest that you report it. As a quick workaround, you could overwrite the conditions in your table class, like
$this->association('Ratings')->conditions([]);
I connect to a remote database from within my symfony2 app with this code
$connectionFactory = $this->container->get('doctrine.dbal.connection_factory');
$conn = $connectionFactory->createConnection(array(
'driver' => 'pdo_mysql',
'user' => 'mattias',
'password' => 'fdfsdf',
'host' => 'fs1.rrtgy.se',
'dbname' => 'csmedia',
));
return $conn;
Is there a parameter I can set to do it using SSL?
The equivalent of something like this:
$link = mysql_connect("192.112.7.18","test","testpass",false,MYSQL_CLIENT_SSL)
You could try add to createConnection array 'driverOptions'
$conn = $connectionFactory->createConnection(array(
'driver' => 'pdo_mysql',
'user' => 'mattias',
'password' => 'fdfsdf',
'host' => 'fs1.rrtgy.se',
'dbname' => 'csmedia',
'driverOptions' => array(
PDO::MYSQL_ATTR_SSL_CA =>'/path/to/ssl-ca.pem'
),
));
More info about MYSQL SSL constants.
Notice, that some constants were added at php 5.3.7
However, SSL options are silently ignored in (at least) version 5.3.8: see the bug report.
I'm building a Website in Cakephp with variable databases.
I did this with the following code:
CONTROLLER:
$db_host = $DB_SET['Project']['db_host']; //From other database
$db_user = $DB_SET['Project']['db_user'];
$db_pass = $DB_SET['Project']['db_pass'];
$db_database = $DB_SET['Project']['db_database'];
ConnectionManager::create("client_db", array(
'datasource' => 'Database/Mysql',
'driver' => 'mysql',
'persistent' => false,
'host' => $db_host,
'login' => $db_user,
'password' => $db_pass,
'database' => $db_database,
'prefix' => '',
'encoding' => 'UTF8',
'port' => '',
));
$DB_LINE = $this->Page->findPage('3');
MODEL:
class Page extends Model {
public $useDbConfig = 'client_db';
function findPage($pagenr) {
$page = $this->find('first', array(
'conditions' => array (
'Page.id' => $pagenr)
));
return $page;
}
}
Now I also need to change of tables in the database on the Fly.
I do this using(Controller):
$tbl_current = array('tbl_cheques', 'tbl_wishes');
$this->Modelname->useTable = $tbl_current[$pageid]; //Getting the pageID from an url parameter.
Everything works fine, only if I don't wait a while before clicking another page I get this error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Page.fqlkdf' in 'field list'
This because Cake still have the previous table in his Cache.
If I wait a minute and then I change the page it works fine.
Any suggestions for this problem?
Thanks in advance,
Aäron
Like you said, the table info is cached. So you just need to remove that cache:
Cache::clear(false, '_cake_model_');
Or, you can just temporarily disable cache
Configure::write('Cache.disable', false);