Morning All --
I'm working on a module where I want to do some DB interactions with an external database. The external database, for the moment, is a 2nd database residing in my MYSQL installation on my local machine.
My settings.php has the following:
$databases = array (
'default' =>
array (
'default' =>
array (
'database' => 'wcpd',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
'mock_banner' => array (
'default' => array (
'database' => 'mock_banner',
'username' => 'root',
'password' => '',
'host' => 'localhost',
'port' => '',
'driver' => 'mysql',
'prefix' => '',
),
),
);
And my module has the following:
function db_test_menu() {
$items['dbtest-page'] = array(
'page callback' => 'db_test_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function db_test_page() {
return "Trying to insert into TBRACCD";
db_set_active('mock_banner');
$insert = db_insert('tbraccd')
->fields(array(
'tran_number' => '123456',
'term_code' => '201302',
'user' => $user->uid,
))
->execute();
return $insert;
db_set_active('default');
When I visit http://localhost/dbtest-page in my browser, the page is displayed and I see my "Trying to insert" text. However, when I look in PHPmyadmin, I see that nothing has been inserted into my external database table. The fieldnames in my table all have tbraccd_ as a prefix. I originally had those defined in my three field names. I removed them and tried it as I pasted above, with the same result.
Could someone tell me if they see something wrong with what I've done? What can I do to trouble shoot further to see if the query is executing successfully or not?
Any help would be appreciated.
Thank you!
-- Chris
Comments
Since thisreturn "Trying to
Since this
return "Trying to insert into TBRACCD";is a return statement, nothing after it executes.
Try moving it to be the last line in the function.
Got me a bit closer!
I changed the return to print. Now it prints out my text and a slew of SQL errors since I forgot to set default values on my columns. I fixed those, now I'm getting an integrity violation on my user column:
Not sure about the integrity error. That column is set to be not null and has a default value.
And some missing tables filter_format and sessions? How would I have known I needed these?
Reading More About Database API
Reading more about the Database API, I now believe that it can/does not work against an external database. so I've changed it to the following:
I've dumbed it down so all I'm trying to insert is one field via the mysql_query command
So I change the active database, execute the command then immediately switch the database back to default.
When I visit the page, I see the text I print out to the screen and nothing else. No layout, no errors. Looking in my external db, nothing is inserted.
So, my questions remain: What am I missing and how can I troubleshoot further?
Any further guidance would be greatly appreciated.
Thank you!
Instead of mysql_query() you
Instead of mysql_query() you want to use db_query().
Another Step Closer
That got me another step closer. Thanks, nevets.
Now, I'm getting errors similar to this
I also tried the method suggested on the page that you linked to from the page above to no avail. I'm sure its something simple that I'm just not seeing.
Is there a method I can use to test for the database connection before I try to run the query? That way I could at least get the page to end gracefully and give me an idea of whats happening rather than bomb out...
I've now taken my external db
I've now taken my external db connections out of settings.php and I have it set up on the fly in the module itself. I also added a not condition to the db_query as follows:
and that worked!