Importing data from another database on Drupal 7

May 7, 2013

Here’s a quick tutorial on how to import data into a Drupal installation using a secondary database. I’ve done this by using an external PHP file located at the root of the Drupal installation and bootstrapping it. You can call it import.php.

All the magic happens by using db_set_active(‘secondary_database’), which connects Drupal to another database. Calling db_set_active() will switch you back to the default one. This can be configured in our settings.php file.

Let’s start with our settings file.

$databases = array(
	'default' => array(
		'default' => array(
			'database' => 'drupal_db',
			'username' => 'root',
			'password' => 'root',
			'host' => 'localhost',
			'port' => '',
			'driver' => 'mysql',
			'prefix' => '',
		),
	),
	'secondary_database' => array(
		'default' => array(
			'database' => 'secondary_data',
			'username' => 'root',
			'password' => 'root',
			'host' => 'localhost',
			'port' => '',
			'driver' => 'mysql',
			'prefix' => '',
		),
	),
);

Let’s do some testing first with our import.php.

<?php
// we need to bootstrap Drupal first
require_once 'includes/bootstrap.inc';
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
print "Importing Entries\n";
// connect to a different DB defined on your settings file.
db_set_active('secondary_database');
// let's get all the entries we need for now.
$query = db_select('tablename', 's')->fields('s', array());
$results = $query->execute()->fetchAll();
print_r($results);

To run this, we can use:

php import.php

This should output an array of objects coming from your second database.

Importing data

Now that we have tested our connection, we can finalize our script:

<?php

// we need to bootstrap Drupal first
require_once 'includes/bootstrap.inc';
define('DRUPAL_ROOT', getcwd());
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

print "Importing Entries\n";
// connect to a different DB defined on your settings file.
db_set_active('secondary_database');
// let's get all the entries we need for now.
$query = db_select('tablename', 's')->fields('s', array());
$results = $query->execute()->fetchAll();

// back to the default database and building nodes
db_set_active();
foreach ($results as $res) {
	$node = new stdClass();
	$node->title = $res->name;
	$node->language = LANGUAGE_NONE;
	$node->type = 'body';
	$node->uid = 1;
	$node->status = 1;
	$node->body['und'][0]['format'] = 3;
	$node->body['und'][0]['summary'] = $res->description;
	$node->body['und'][0]['value'] = $res->description;
	node_save($node);
}

And that’s it, now you should be able to import entries from other databases by bootstrapping Drupal and connecting to it temporarily. All the new content will be stored in a Drupal way.

This is a cross post that is also published at HybridForge Blog

Posted in:

Living with a PS Vita

Mar 8, 2013

psv-lbp-ss4

Smartphones are taking over mobile gaming, we all know it, but they still don’t offer the biggest feature for most hardcore gamers: analog controls. While some puzzle games play well with touch screens, everything else plays just bad: Shooters, FPS, beat-em-ups. So, Sony had the best idea ever: Merge all the goodness from a smartphone and bring it over to the PSP. The result is a beautiful device with a lot horsepower, good controls, touch screen and a touchpad at the back. It is really the device to have if you want to game on the go.

(more…)

Posted in:

Framework or CMS, choosing the right tool

Feb 2, 2013

integrate-analytics-with-cm

Working mainly on Internet marketing I get two types of projects to work on: simple client websites or medium size websites with web application features. Any of them will have a limited budget, so my task as developer is to grab the best toolset and pick the right tool for it.

I could list the good and bad things from a CMS or framework but I will rather put an example:

Our website doesn’t have a lot of content but it has to connect to a third party service to manage subscriptions for our courses.

Let’s say the site has 15 pages with text and pictures and a pretty regular layout, which won’t change very often. Then, we have a 3rd party API, using SOAP services where we have to list a few courses and collect some registration information and payment. Every request and payment handled by the API.

If we use a CMS in this case, we will speed up the theming, menu structure, file uploads and content editing. Creating the courses section means we need to create custom routing, multiple forms, error messages, and a class that interacts with the third party application.

CMSes most of the time don’t have Forms API, validation API, error notifications or proper custom routing. This leaves you with trying to build some sort of web app functionality using basic PHP. Hello 90′s! (more…)

Posted in:

My Windows 8 Review

Nov 16, 2012

I don’t usually write reviews of OSes or software of any kind, but I felt like writing about this upgrade to Windows that have been getting a lot hype and a lot of reactions from users and media.

TL;DR: I quite like it, it’s an improvement on almost every way on the way I use Windows and it actually gives me a lot of reasons to go back to Windows as my main OS.

As a power user (that’s how I consider myself), I will focus on my workflow exclusively. There are quite a lot of reviews out there that mention a lot of casual and light users problems or frustrations with the OS, which I can’t care less to honest. In my opinion, those are comments from power users trying to think as a casual user, which they are not.

Start Menu

This is by far the most talked item about Windows 8. It’s different, big, tiled and very “touch based”. If I stop for a moment and think about what I used the start menu for, the new one is much better. Press Win+Q and start typing any app that you want and press Enter to open it.

Organizing apps is something I used to do with the old school Start menu (Windows 98 version), and I hated when in Vista/7 they made it all go into one small box. I welcomed the fast indexing of apps so you could start typing the app name, but I still missed the the fact that navigating through that Start menu became totally unfriendly. Windows 8 makes that better by offering an even faster indexing and grouping is pretty cool.

Modern/Metro apps and Windows Store

Simply put: If you don’t like it, don’t use it! I personally like it a lot. It’s allowing all that discovery that you get on platforms like iOS, Android and even OSX into Windows. I was hoping for them to have a standardized way to install desktop apps through the store but they decided to only allow modern apps on it. A bummer, but that’s the direction they want to move into.

I haven’t had a lot of time to download and test apps from the store but so far I love both MetroTwit and Reddit To Go! There are quite a lot of games for free too that I will be spending time with.

Another thing I like from the store is that it knew the apps I used on another Windows installation so it listed them under “My Apps”. Same as what Android does, but better because you can check a bunch and install them all at once.

(more…)

Posted in:

Sager NP9150 Review

May 20, 2012

Let’s start by saying that I’m coming from a Macbook Pro, which I still use for Android development and other things. The idea to buy a super powerful laptop came to me while on my last vacations in Chile, when I finally had the time to game a lot and, sadly, I had to deal with the poor performance of my Macbook Pro. So I decided to buy something powerful enough to play all recent games and after reading for quite a while I decided to go with Sager, mainly because they seems to give you the best performance for the money you pay. I didn’t go with Asus because they don’t use the fastest video cards and Alienware because they are just plain ugly.

(more…)

Posted in: