Subversion hosting, CVS hosting, Trac hosting, Bugzilla hosting and software collaboration Providing hosted Subversion, CVS, Trac and Bugzilla repositories
 

February 20, 2008

Hooking My CodeIgniter Application to MySQL

Filed under: Software Development — Tags: , , , — Greg Larkin @ 7:19 pm

Hi everyone,

When I last posted about my sample application development with CodeIgniter, I had created a simple Hello, World application. The next thing I want to explore is how easy it is to connect to a MySQL database and retrieve data.

As a quick side note, the CodeIgniter User Guide is well-written and very clear with plenty of examples. That’s always nice to see and instills some confidence in the quality of the underlying code.

According to the guide, all of the database connections should be defined in the application/config/database.php file. After setting up a new database named “ci” and a database user, I opened database.php and set up my connection:

$db[‘default’][‘hostname’] = "localhost";
$db[‘default’][‘username’] = "ci";
$db[‘default’][‘password’] = "ci";
$db[‘default’][‘database’] = "ci";
$db[‘default’][‘dbdriver’] = "mysql";
$db[‘default’][‘dbprefix’] = "";
$db[‘default’][‘active_r’] = TRUE;
$db[‘default’][‘pconnect’] = TRUE;
$db[‘default’][‘db_debug’] = TRUE;
$db[‘default’][‘cache_on’] = FALSE;
$db[‘default’][‘cachedir’] = "";

That was easy enough. Now I’ve got the skeleton of a model defined in application/models/user_model.php:

class Group_model extends Model {
    function Group_model()
    {
        parent::Model();
        $this->load->database();
    }
}

The $this->load->database() statement automatically connects to MySQL using the default connection I defined previously. Now I can use the $this->db object to run queries and process results elsewhere in my model file.

I’ll replace my original hard-coded count_users function with something that runs an actual database query:

function count_users()
{
    $query = $this->db->query(‘SELECT COUNT(*) AS num_users FROM user’);
    if ($query->num_rows() > 0)
    {
        return $query->row()->num_users;
    }
}

Of course, I have to leave a newbie coding error in there for someone to notice. Anyone? Anyone? Bueller?

Here’s my simple MySQL database schema:


– Table structure for table `user`

CREATE TABLE IF NOT EXISTS `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_name` VARCHAR(128) NOT NULL,
`full_name` VARCHAR(128) NOT NULL,
`email` VARCHAR(128) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;


– Dumping data for table `user`

INSERT INTO `user` (`id`, `user_name`, `full_name`, `email`) VALUES
(1, ‘glarkin’, ‘Greg Larkin’, ‘glarkin@sourcehosting.net’),
(2, ‘morsel’, ‘Morsel The Cat’, ‘morsel@meow.com’);

When I navigate to my /codeigniter/user/count_users/ URL, I see:

There are 2 users defined

Great! Next up, we’ll take a look at creating an actual output page with views.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 16, 2008

VMware Server Watchdog Timeouts on Host-Only Interface

Filed under: Operating Systems — Tags: — Greg Larkin @ 11:33 pm

Hi everyone,

I installed a bunch of security updates, new kernels, etc. to the VMware Server hosts and virtual machines tonight. One problem that has been dogging me for a while now is that one (and only one) of the VMs boots in a strange state and reports Ethernet watchdog timeouts on its host-only interface. The issue here is that it’s then unable to communicate with its name server, NFS server and other LAN resources.

After some different Google searches, I found a solution provided by Antonio Lorusso. I’ve made his changes to the VMware Server file in question, and I’ll keep an eye on the VMs as they auto-start after the next host server reboot. I expect that the timeouts will be a thing of the past. Thank you, Antonio!

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 15, 2008

Telecommuting PHP Developer Position

Filed under: Jobs — Tags: , , — Greg Larkin @ 4:38 pm

Hi everyone,

SourceHosting.net recently started a professional services contract for a new client in Maine, and they  need a PHP developer to finish a project for a client of theirs. The system is a content management and e-commerce web site used to order and fulfill scientific supplies. The site is targeted to a typical LAMP stack.

I had a look at the database schema and the existing code, and it is surprisingly well-commented, indented nicely and follows a naming convention. That was a nice change of pace! I suspect that it won’t be too difficult to pick up what’s there and learn it well enough to finish the project.

This project is available to an off-site developer, and there is some time pressure to get it done. They would like a person who can work full-time over the next few weeks. If you are interested, please reply directly to me with an introduction, your resume and a description of recent LAMP projects. If that all looks good, I’ll put you in touch with the hiring manager.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

CodeIgniter 1.6.1 Is Released

Filed under: Software Development — Tags: , , , , — Greg Larkin @ 4:31 pm

Hi everyone,

I just saw that CodeIgniter 1.6.1 has been released. I guess that means I’d better get on the stick and upgrade the FreeBSD CodeIgniter port! Once I do that, I’ll upgrade my PHP Framework VM with the new version of the port and continue on with the experimentation.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 11, 2008

Definition Of An Unqualified Lead

Filed under: Business — Tags: , — Greg Larkin @ 11:50 am

Greetings everyone,

I’m always excited to see a new RFQ inquiry come in from the SourceHosting.net Professional Services RFQ page. Generally, the prospect needs help with an internally-hosted source code control system, some release management consulting or perhaps a solution to a particularly thorny infrastructure problem that’s impacting their software developers. There’s always a good opportunity to flex the ol’ brain cells and learn about a new environment.

However, within the past couple of weeks, I’ve received 2 inquiries from somewhat confused visitors. I’m posting them here for your amusement (names changed to protect the, um, clueless). Anyone care to hazard a guess how these folks landed at SourceHosting.net?

From: confused_person@Yahoo.com
Sent: Saturday, January 26, 2008 11:49 AM
To: support@sourcehosting.net
Subject: [SHN] Pro Servs RFQ

Name:         Confused Person
Company name: cvs
Email:        confused_person@Yahoo.com
Phone:        8005551212
Request:      Everytime I visit cvs my receipt says Your
Extracare account information is incomplete .Please visit web
site I have tried this number of times I'm have problem where to go.

Additional information
Number of users:
Number of remote users:
Repository size:
Does backups:           N/A
Number of servers:
Server types:           Windows
Tools in use:           CVS

I sent this person a polite note with a pointer where he most likely should have directed his inquiry and had a good laugh.

This morning, I got 2 inquiries from the same person, so maybe this is going to be a trend!?

Date: Sun, 10 Feb 2008 11:30:59 -0500 (EST)
To: support@sourcehosting.net
Subject: [SHN] Pro Servs RFQ
From: I_am_confused@sbcglobal.net

Name:         Confused Person
Company name: self0employe
Email:        I_am_confused@sbcglobal.net
Phone:        800-555=1212
Request:      Complaint at the Nowheresville, Nostate Store.
I was treated by Anonymous (sore empolyee) wrongly this morning

Additional information
Number of users:
Number of remote users:
Repository size:
Does backups:           N/A
Number of servers:
Server types:
Tools in use:

And then minutes later:
*** *** ***
Anonymous (store employee) treated wrongly this morning she
question my ID .I have been going to this store for 21 years
*** *** ***

Bummer – the sore, er, store employee should be reprimanded!

If you’ve got some amusing CCRM (confused customer relationship management) tidbits, feel free to share them here!

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 8, 2008

Diving Into CodeIgniter

Filed under: Software Development — Tags: , , , — Greg Larkin @ 5:19 pm

Hi everyone,

Now that I’ve got my FreeBSD virtual machine set up with the various PHP frameworks, it’s time to start putting together a sample application in each one to learn their strengths and weaknesses. The sample application will be a simple mailing list manager and contain common features such as:

  • User account self-registration
  • Email generation
  • Management interface
  • MySQL database backend

I think that will exercise enough of each framework to get a good handle on their functionality and ease of use.
I’m starting off with CodeIgniter. At first glance, the documentation seems excellent, and the code is commented very clearly. Looking at the index.php file in the root installation directory (/usr/local/www/codeigniter on FreeBSD), I see:

/*
|—————————————————————
| APPLICATION FOLDER NAME
|—————————————————————
|
| If you want this front controller to use a different "application"
| folder then the default one you can set its name here. The folder
| can also be renamed or relocated anywhere on your server.
| For more info please see the user guide:
| http://www.codeigniter.com/user_guide/general/managing_apps.html
|
|
| NO TRAILING SLASH!
|
*/
$application_folder = "application";
 

Ok, that seems easy enough. To keep the default application intact and start working on my custom application, I simply copied the existing application directory to /usr/local/www/ci_sourcehosting and updated the index.php file.

$application_folder = "/usr/local/www/ci_sourcehosting";
 

According to the welcome page displayed by CodeIgniter, I have to change the views/welcome_message.php file to change the default page contents. Ok, easy enough…

Custom Hello World on CodeIgniter

The controllers/welcome.php controller is called by default, and that’s configured in the config/routes.php file in your application directory.

To create a new controller, simply create a new class that extends Controller and name it with a capital letter. Save the new class to the lowercase version of the class name into the /controllers directory, and you’re ready to test.

My simple class is:

class User extends Controller {
    function index()
    {
        echo ‘Welcome to the user controller’;
    }
    function count-users()
    {
        echo ‘There are 0 users defined’;
    }
}
 

When I visit http://192.168.95.128/codeigniter/user, I see:

Welcome to the user controller

and http://192.168.95.128/codeigniter/user/count-users displays:

There are 0 users defined

Ok, that’s pretty straightforward, but I did have to do a bit of mucking about to get the URLs to look like http://192.168.95.128/codeigniter/user/count instead of http://192.168.95.128/<strong>index.php</strong>/codeigniter/user/count.

The CodeIgniter manual specifies a .htaccess file to be placed in the directory where the index.php front controller is located:

RewriteEngine on
RewriteCond $1 !^(index.php|images|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
 

Normally, this works fine, except Apache 2.2 specifies AllowOverride None by default, so .htaccess files don’t work. After changing that, I had to make one small change to the .htaccess file, since my CodeIgniter installation is rooted at /codeigniter:

RewriteRule ^(.*)$ /codeigniter/index.php/$1 [L]
 

Next time, we’ll take a look at hooking CodeIgniter to MySQL.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 6, 2008

RSS Feed for FreeBSD /usr/ports/UPDATING

Filed under: Operating Systems — Tags: , — Greg Larkin @ 10:44 am

Hi everyone,

Since I’ve transitioned to FreeBSD, one thing I haven’t done regularly is keep track of the /usr/ports/UPDATING file. This file contains important information about incompatible changes to ports that could cause a port upgrade to break or yield unexpected results. Therefore, it’s important to check it before running a wholesale upgrade on a production server.

I’ve been using Awasu as my desktop RSS reader, and every time I find something I want to keep track of without having to remember to do so, I look for an RSS feed. The contents of the UPDATING file seem a natural fit for delivery as an RSS feed, but a few Google searches yielded no results.

Finally, I went straight to the freebsd-ports mailing list archive, and simply searched for “rss”. Lo and behold, Alex Kapranoff set up a feed back in 2005: http://lists.freebsd.org/mailman/htdig/freebsd-ports/2005-June/024285.html

The feed URL is http://kapranoff.ru/~kappa/files/ports.UPDATING.rss20.xml, for anyone who’s interested. Thanks Alex!

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

February 1, 2008

Installing PHP Frameworks with the FreeBSD Ports Tree

Filed under: Software Development — Tags: , , , , — Greg Larkin @ 11:21 am

Hi everyone,

Ok, let’s proceed to install the various PHP frameworks that we’ll use to create a sample application.

As I mentioned in a previous post, the basic command for installing any port in the tree is:

cd /usr/ports/&lt;category&gt;/&lt;appname&gt; &amp;&amp; make install clean

Let’s start with the CakePHP framework:
cd /usr/ports/www/cakephp &amp;&amp; make install clean

After typing that command, you should see this on the screen:

FreeBSD Port Installation Options

Hmm, what is that screen for? The FreeBSD ports collection has a robust infrastructure for configuring software applications prior to installation. In this case, CakePHP works with multiple different database backends, so the port writer (i.e. me) decided to provide options so the prerequisite bits are installed before the port proper.

Looking at the options provided here, the first one named “PROD” determines the way that the Apache web server is configured for CakePHP. If this is a production server, this option should be selected so navigating to “http://<servername>/” displays the CakePHP welcome page. Since I am installing multiple frameworks on the same machine, I’ll leave this option unchecked. That way, each framework welcome page will be found at “http://<servername>/<frameworkname>/”.

I plan to use MySQL as a database backend, so the following screenshot shows the MYSQL option selected. The dialog box is navigated with the tab and arrow keys on your keyboard, and the spacebar toggles the option selections:

FreeBSD Port MySQL Option

Tab to the OK button and hit Enter, and the port build starts. Here is the full transcript of the process:

CakePHP Port Build Transcript

Notice that by selecting the MYSQL option prior to installation, the port build fetched and built the MySQL client library and the PHP MySQL module, as well as some other packages:

mysql-client-5.0.51
php5-pcre-5.2.5_1
php5-pdo-5.2.5_1
php5-pdo_mysql-5.2.5_1
php5-session-5.2.5_1

This helps us get a working PHP installation that supports CakePHP out of the box.

Now, it’s the moment of truth – does the CakePHP default page display correctly? In order to load the page on your host machine, find the VM’s IP address with the following command:

/sbin/ifconfig -a | grep -w inet | grep -v 127.0.0.1 | awk '{ print $2 }'

That gives me 192.168.95.128, so here’s the URL where I should find the CakePHP default page: http://192.168.95.128/cakephp/. The address will likely be different for you.

This looks good!

CakePHP Default Page

Now as an exercise for the reader, try installing the rest of the frameworks and make sure that they display their welcome pages:

CodeIgniter
PRADO
Zend Framework

If you have any trouble, write in with comments and feedback.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

January 31, 2008

Apache Auto-configuration with a Dynamic IP Address

Filed under: Operating Systems — Tags: , , — Greg Larkin @ 6:02 pm

Hi everyone,

As I was setting up the new FreeBSD 6.2 VM with Apache 2.2 preinstalled in it, I ran into a problem. The VM’s virtual Ethernet adapter is configured for DHCP, and the VMware software acts as a DHCP server to hand out IP addresses as VMs are started.

The problem is that Apache likes to know the IP address of the machine it’s running on, and in some cases, it won’t start unless it does. Luckily, the DHCP client in FreeBSD, and likely other operating systems, calls separate hook scripts (if they exist) before and after configuring the adapter with a dynamic address.

In my case, I wanted to automatically create a ServerName directive for Apache with the dynamic address. The default Apache 2.2 installation on FreeBSD provides a user configuration file include directory at /usr/local/etc/apache22/Includes. As it starts up, Apache loads any file placed in that directory and named with the “.conf” suffix. I think we’ve got a solution!

It’s also necessary to tweak the contents of the /etc/hosts file so that the VM can resolve its own hostname. I wrote a DHCP client exit hook script that configures both Apache and the /etc/hosts file. This script should be easily adaptable to other operating systems, as it uses the standard Bourne shell syntax.

Simply download the script, unzip the contents into /etc, and let me know what you think!

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error

Updated FreeBSD 6.2 VMware Images And BitTorrent

Filed under: Operating Systems — Tags: , , , — Greg Larkin @ 5:28 pm

Hi everyone,

As I got further into configuring my sample VM, I decided it would make sense to provide a few different VM flavors for folks who don’t want to configure Apache, PHP, and other ports to get the PHP frameworks installed and working.

I’ve set up a BitTorrent tracker at http://torrents.sourcehosting.net:10692/, and I’ll place the various VMs there from now on. The installation process is as follows:

  • Download and install a BitTorrent client, if you don’t already have one. I recommend Azureus.
  • Grab the UsrPorts torrent.
  • Grab a FreeBSD 6.2 Basic Installation torrent, such as the Bare Bones VM, or the VM pre-configured with Apache. Check the main tracker site for other options, and view the torrent info page to find out what ports have been preinstalled.
  • Once the UsrPorts.zip file has been fetched by your BitTorrent client, extract the contents to a new directory.
  • Extract your selected FreeBSD 6.2 VM .zip file to the same directory and start up the VM with VMware Player or VMware Server.

The UsrPorts.zip file contains the /usr/ports filesystem that’s mounted in the VM. Since this filesystem is typically kept up to date by running portsnap while the VM is running, I’ve provided an initial snapshot for you, but it’s not necessary to keep re-downloading when I provide a new FreeBSD VM that you’d like to try out.

I hope this system works better for folks, and please consider seeding the torrents as you download the .zip files.

Bookmark and Share

Keep in touch,
Greg
SourceHosting.net, LLC



Call me - Greg Larkin: error
Pages: Prev 1 2 3 4 5 6 Next

Powered by WordPress