Number Keypad Doesn’t Work on Ubuntu VirtualBox

I don’t do too much with Ubuntu. What little I’ve seen, I like, but I just haven’t had time to try to do much.

I do run one virtual machine, that I use as a web/mysql server for my development/test environment. It’s linked to the directory where I edit files, which saves me the step of uploading new code after every change. I can just save the file, hit refresh on the browser, and go.

One of the only things that bothered me is that the number keys on the keypad didn’t work in the virtual machine. I didn’t know if it was an Ubuntu issue, a VirtualBox thing, or something that would only happen when you use those two in concert.

I turns out that it’s working the way Ubuntu intended. The fix is really easy. Just one little configuration box to uncheck:

  1. System -> Preferences -> Keyboard.
  2. Click the Mouse Keys tab.
  3. Uncheck the box that says “Pointer can be controlled using the keypad.”
Posted in Linix/Apache | Leave a comment

On Flexibility

This post is less technical/nerdy, but I just wanted to provide a general endorsement of something that I see becoming more and more common. Specifically, I’ve been dealing with complex systems, and along with the normal bevy of settings, there are also little places where educated administrators can write code to do something that the developers couldn’t originally anticipate.

I started designing systems with this sort of feature a number of years ago. We were building interfaces from a loan origination system to set up new loans on bank or credit union servicing systems. It seems easy to do and to replicate, but in reality, every company used both systems differently — sometimes radically differently.

So that we wouldn’t have to rewrite a specific interface from one client to another, we started making configuration items that implementation users could configure. Back then, it was often SQL statements that would get submitted to the server. The program could blindly submit whatever was in the box, and assume that the result that came back from the server was the desired result. We used settings like this to override default data mappings, and even to define process flow.

One of the projects I’ve worked on for the last couple of years is a system used to deploy quizzes and surveys on websites or as Facebook applications. I designed it with the same principals. Now, for any question, I can do calculations and make part of the question include information answered in prior questions. I can redefine multiple choice options on the fly, based on the user’s age, shoe size, or favorite flavor of ice cream (provided that those are questions that we asked or can glean from their social networking profile). Try to do that with Survey Monkey!

But I see this sort of design more and more, in any number of different kinds of systems. And I just want to tip my cap to all of the developers to make the extra effort to make their products so flexible.

Posted in Software/Product Design | Leave a comment

Simultaneous MySQL DB Connections in PHP

I recently added a copy function to one of the web applications that I’ve developed. In addition to letting the user copy records to a new set of records in the same system, I decided to also allow them to copy to other instances of the system. My thought is that administrators can publish their configurations directly from a test environment to a production environment without needing me to generate a script and move data from system to system.

Having already completed the code to select the data, insert new records, and save those keys for future inserts, the most difficult part was connecting to two MySQL databases at once.

But in fact, the mysql_select_db() and mysql_query() functions take an optional second parameter to let you specify which database handle gets used for the query.

To query database 1, it’s as easy as

and for database 2,

Posted in mysql, php, Web Apps | Tagged , , | Leave a comment

SQL Queries Using with nolock

Last year, I had an assignment to create some reports using SQL Server Reporting Services (SSRS). The database team at this company had created a reporting database, using data cubes, with information rolled up from a number of production systems.

In some cases, the queries were complex, and the reports were created using stored procedures as data sources.

The company had well-defined standards for the structure of the procedures, the naming conventions, etc.

All very normal, so far. Right?

The odd thing in their standard was the directive that the (nolock) instruction be used on any selects from permanent database tables. So queries had to look like,

Let’s take a look at what (nolock) does.

In the normal course of selecting data from a table, the select statement will put a SHARED lock on a table. This type of lock will allow other SHARED locks to run at the same time, but it will block EXCLUSIVE lock transactions from happening before that lock is removed.

In simpler terms, you can run as many select statements on data as you would like. They won’t stop each other. But they will prevent updates, inserts, or deletes from occurring while they are in progress.

Conversely, if the data in a table is being modified, the lock is EXCLUSIVE. Select statements won’t be able to run until those updates are complete.

The (nolock) hint tells SQL Server to go ahead and run the select anyway. During the course of the select, information may be modified.

When a statement modifies data, it also modifies indexes. Data may be moved from page to page. It may be broken by the server in unpredictable ways. While the EXCLUSIVE lock is on, the information may be stored in two places at once. There may be moments where it isn’t in the table at all. This is well documented. SQL Server Books Online says, “When this option is set, it is possible to read uncommitted modifications, which are called dirty reads.”

So if you need accurate, detailed information, you should never, never use nolock. I can’t really ever think of a case where I would recommend it.

The kicker in this case is that the database is just used for reporting. Updates occur in off-hours, so by just using regular select statements, with their regular shared locks, there will be no blocking.

Posted in MS SQL, Transact SQL | Tagged , , , , , , , | Leave a comment

Output Buffers Are too Cool

All php developers have been there. You’ve got the page working just like it should. And with one exception, it looks good too.

That exception: All of your status and debut messages. You hate to get rid of it. It’s got lots of useful information that you wouldn’t mind seeing every time you hit the page. But you know that it would make users throw up their hands in frustration. Worse yet, it might give away enough information to let a hacker do some unpleasant things on your site.

You can go through and comment out each print statement, and that’s exactly how I’ve normally handled it.

But this week, I came up with something better.

The output buffer is a wonderful php construct. It starts with the function call “ob_start();”. From here, all of the printed output goes to the output buffer instead of to the user’s screen.

Presumably the routine to generate the content prints all of my ongoing statuses, and for me, it’s probably full of “print_r($Array);” calls — the php developer equivalent of windows developers inspect variable frames.

“ob_get_contents()” lets you use the contents of the output buffer, and “ob_end_clean()” clears it all out.

In this example, I use the session variable for ‘user’ (which, of course, had been set somewhere earlier in the page) to make that output buffer information print if it’s me who is logged into the site. Otherwise, the user doesn’t see it.

It also makes a lot of sense to use this to write debug information to a file.

Posted in php, Web Apps | Tagged , , , , | Leave a comment

PHP Shortcut to Set Two Values at Once

I’ve been coding in php for seven years, and doing it professionally for four. Until today, I never knew you could use a syntax like this to set the value of more than one variable.

I’m not sure why I’m so giddy about finding this shortcut. Is it really so much more troublesome to type out something like,

It took two extra keystrokes. A semicolon and a stroke of the enter key. But for some reason, I’m giddy like a schoolgirl.

Posted in php, Web Apps | Tagged , | Leave a comment

Find Nth Level of URL from Current Page

I ran into an interesting need when updating some code for MyQuizWiz, an online quiz and survey system that I developed.

There’s a need to display content generated by the system in users’ Facebook fan pages.. Specifically, there is a page where one of the pages pops up a box with the HTML code for an iframe. The end users would copy the code, and paste it into the configuration for their facebook fan page.

The problem I had was that the page whose URL I needed to display in the pop up was several folders up in the url from the page that displays the link. I don’t necessarily know where the system will be deployed, so the path could include any number of sub-domains or sub-folders that are needed in the URL before you get to my root directory.

I was surprised to see that I didn’t already have a function written to take care of this, but I remedied that this morning.

Let’s break down what happens here.

First, the input parameter. I wanted this to work so that by default, it gives the current URL path, but without any file name or request info (the ?variable=x&varible2=y stuff that sometimes appears at the end). If I pass a one, I want it to go up one level from there; two should go up two levels from there. This can continue indefinitely.

This line gets the full and complete URL of the current page. It’s complicated because it uses either SERVER_NAME or REQUEST_URI in the $_SERVER PHP variable, so that it gets the answer no matter what the version of PHP. It’s also complicated by the fact that it can use either HTTP or HTTPS as the protocol.

The for loop iterates through each of the levels, as passed in the parameter. Each time it runs, this function shortens the path:

substr takes the segment of the string ($path) that starts at position zero, and goes to the position where the last instance of ‘/’ is encountered. The strrpos function is just like the strpos function (finding a particular character or string inside of another string), except that it starts at the back of the string and works forward. This would have been a much more difficult function to write without PHP’s strrpos.

Just for grins, I confirmed that the whole function could also be written in one single line. I opted for the way I wrote it for the sake of readability, but this works as well.

Posted in php, Web Apps | Tagged , , | Leave a comment