Trying Baby Steps With Database API

1 reply [Last post]
ccorbett
Offline
Last seen: 4 years 44 weeks ago
Joined: 2013-03-27

Morning All --

After watching Chris's videos on the Database API, I started 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

wileysegovia
Offline
Last seen: 3 years 7 weeks ago
Joined: 2016-01-19
Case sensitive?

Did you make sure the DB name, connection name, etc. all are matching for uppercase/lowercase?

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.