Tag Archives: LinkedIn

How to get the contents of an Oracle CLOB data field in PHP

The Oracle “CLOB” (Character Large Object) is a data type used to store up to 4 Gigabytes of text. Retrieving the contents of a CLOB is not as intuitive as you might think.

Let’s say you have a CLOB field/column named “mychars” in an Oracle DB table named “mytable” along with some other fields. You want to simply echo out the text in the “mychars” field:

<?php
    $id = '3';
    $conn = oci_connect('myusr', 'mypass', 'mydb');
    if (!$conn){
        echo 'Connection error.';
    }
    $sql = 'SELECT * FROM mytable WHERE myid=:id';
    $stid = oci_parse($conn, $sql);
    oci_bind_by_name($stid, ":id", $id);
    $result = oci_execute($stid);
    if($result !== false){
        while($row = oci_fetch_assoc($stid)){
            echo $row['mychars'];
        }
    }
?>

The above code will give you an error that looks like the following:

Catchable fatal error: Object of class OCI-Lob could not be converted to string in somefile.php on line 14

If you try to do a print_r() on the CLOB in an attempt to figure out what you are dealing with you will get something that looks like:

OCI-Lob Object ( [descriptor] => Resource id #3 )

This is because a Lob object is returned instead of the contents of the CLOB.

To get the CLOB contents you will need to call the load() or read() methods on the returned object. The latter will require the length of data to read in bytes but has the advantage of not being limited by the script memory limit:

<?php
    $id = '24382';
    $conn = oci_connect('myusr', 'mypass', 'mydb');
    if (!$conn){
        echo 'Connection error.';
    }
    $sql = 'SELECT * FROM mytable WHERE myid=:id';
    $stid = oci_parse($conn, $sql);
    oci_bind_by_name($stid, ":id", $id);
    $result = oci_execute($stid);
    if($result !== false){
        while($row = oci_fetch_assoc($stid)){
            echo $row['mychars']->load();
            //or
            echo $row['mychars']->read(2000);
        }
    }
?>

How to add a custom context menu to a Spark TextArea in Flex 4

There is a current known issue with adding custom context menus on a RichEditableText Spark component:
http://bugs.adobe.com/jira/browse/SDK-23926

This includes the TextArea component. Essentially, any custom context menus will not show up. There is a work around mentioned in the comments for the bug on Adobe’s website but I thought I would re-hash and show an example since this had me a bit stumped.

The work around is to attach the context menu to the TextArea’s TextDisplay object via the “textDisplay()” accessor method. I have created a simple example with source.

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="400" minHeight="200" backgroundColor="#9FA0D4" width="500" height="300"
               creationComplete="init();" viewSourceURL="srcview/index.html">
    <s:layout>
        <s:BasicLayout/>
    </s:layout>
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <fx:Script>
        <![CDATA[
            private function init():void{
                var cm:ContextMenu = new ContextMenu();
 
                var red:ContextMenuItem = new ContextMenuItem('Red',false,true,true);
                red.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleRed);
 
                var green:ContextMenuItem = new ContextMenuItem('Green',false,true,true);
                green.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleGreen);
 
                var blue:ContextMenuItem = new ContextMenuItem('Blue',false,true,true);
                blue.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, handleBlue);
 
                cm.customItems.push(red);
                cm.customItems.push(green);
                cm.customItems.push(blue);
                cm.clipboardMenu = true;
                textArea.textDisplay.contextMenu = cm;
            }
 
            private function handleRed(e:Event):void{
                textArea.setStyle('contentBackgroundColor', 'red');
            }
 
            private function handleGreen(e:Event):void{
                textArea.setStyle('contentBackgroundColor', 'green');
            }
 
            private function handleBlue(e:Event):void{
                textArea.setStyle('contentBackgroundColor', 'blue');
            }
        ]]>
    </fx:Script>
    <s:TextArea id="textArea" x="161" y="74" text="This is a Flex 4 Spark TextArea.  Right click to see the custom context menu. "/>
</s:Application>

SWFObject flashvars, params, and attributes

I have been working with SWFObject a bit lately and was a little confused by how the flashvars, params, and attributes arguments for the embedSWF() function differ from each other and what exactly SWFObject did with them. Based on the documentation, I did a little experiment where I used each and then viewed the generated source using the Web Developer Toolbar for Firefox.

Here is my HTML and SWFObject JavaScript which uses the dynamic publishing method as described in the SWFObject Documentation:

<html>
 
<head>
<script type="text/javascript" src="swfobject/swfobject.js"></script>
</head>
 
<body>
<div id="embedhere">Embed Fail</div>
<script type="text/javascript">
 
	var flashvars = {};
	flashvars.my_flashvar1 = "my_flashvar_value1";
	flashvars.my_flashvar2 = "my_flashvar_value2";
 
	var params = {};
	params.my_param1 = "my_param_value1";
	params.my_param2 = "my_param_value2";
 
	var attributes = {};
	attributes.my_attribute1 = "my_attribute_value1";
	attributes.my_attribute2 = "my_name_attribute";
 
	swfobject.embedSWF("Test.swf", "embedhere", "300", "250", "9.0.0", false, flashvars, params, attributes);
 
</script>
</body>
 
</html>

Here is what the generated source looks like after I load the page in Firefox and SWFObject has done its thing:

<html><head>
 
 
<script type="text/javascript" src="swfobject/swfobject.js"></script>
<style media="screen" type="text/css">#embedhere {visibility:hidden}</style></head><div firebugversion="1.5.0" style="display: none;" id="_firebugConsole"></div><body>
<object data="300x250TestAdAS3.swf" name="my_name_attribute" id="my_id_attribute" type="application/x-shockwave-flash" height="250" width="300"><param value="my_param_value" name="my_param"><param value="clickTAG=http%3A//www.example.com" name="flashvars"></object>
<script type="text/javascript">
	var clickTAG = escape("http://www.example.com");
	var flashvars = {clickTAG:clickTAG};
 
	var params = {
		my_param: "my_param_value"
	};
 
	var attributes = {};
	attributes.id = "my_id_attribute";
	attributes.name = "my_name_attribute";
 
	swfobject.embedSWF("Test.swf", "embedhere", "300", "250", "9.0.0", false, flashvars, params, attributes);
</script>
</body></html>

As you can see the attributes are just attributes to the object tag. The parameters show up inside of param tags with one param tag for each param following the object tag. I would be curious to hear if anyone has used the “attributes” and “parameters” arguments when embedding Flash with SWFObject and for what.

The flashvars argument is the most useful in my case because that is how you can get external data passed into the SWF. I think a lot of folks load in their data into a Flash movie using a separate call to load an XML document. That is the way to go if you are pulling a large amount of data dynamically based on some type of user input.

If you only have a few name value pairs that are not going to change, passing these in during the embed using flashvars is probably the better option. Assigning these values during the embed will save a second round trip you would normally make to get an XML document.

So let’s say you decide you want to pass in a couple key-value pairs to a Flash movie using SWFObject. In the code example above there are two key-value pairs we assign to flashvars: “my_flashvar1″ and “my_flashvar1″. Now you will want access these two inside your ActionScript code.

For ActionScript 2 it would look something like this:

trace(_level0.my_flashvar1);
trace(_level0.my_flashvar2);

ActionScript 3 requires another line of code to get to the same place:

var paramList:Object = this.root.loaderInfo.parameters;
trace(paramList["my_flashvar1"]);
trace(paramList["my_flashvar2"]);

A word of caution: When you use SWFObject’s embedSWF() function there are quite a few optional parameters. Be sure that you put a “false” in for optional parameters you don’t intend to use before any parameters you ARE going to use. In the embed example below I want to use the flashvars option but I don’t want to use the option to specify an express install file so I put in a false in that spot. Also, just be careful in general because there are a total of 10 different arguments for the embedSWF() function (including all the optional ones) so it is easy to get them in the wrong order and such.

swfobject.embedSWF("Test.swf", "embedhere", "300", "250", "9.0.0", false, flashvars);

Take a look at the SWFObject documentation for a description of each parameter.

That’s it!

Must-have web application development tools

I have come up with a list of my “must-have” development tools:

  1. Dual Monitors – Developing with two monitors will make you much more productive simply because you spend less time switching between windows all day if nothing else. Monitors are pretty cheap and the productivity gains will more than pay for a dual output video card and second monitor. Even Microsoft says so.
  2. Firebug – If you do any kind of web development you should have Firebug on your tool belt. This Firefox Add-on will tell you exactly what CSS properties are being applied to an HTML element and from where and then allow you to change those properties on the fly in the browser. It also offers JavaScript debugging, a DOM tree inspector, and last but not, least, a “Net” panel that allows you to see all your browser requests, responses, and times.
  3. IE Developer Toolbar – IE’s answer to Firebug. It is not quite as full featured as Firebug in my opinion but it does at least allow you to inspect an element and determine how styles are being applied to it and where. This is quite useful since IE has a different box model than Firefox and you can use all the help you can get when trying to make a site look the same in both browsers.
  4. Charles Web Debugging Proxy Application – Charles acts as an intervening proxy to your web browser that records all the requests and responses. Some of this functionality overlaps with Firebug but Charles goes a bit further by providing request breakpoints, request editing, throttling, and DNS spoofing.
  5. Firefox Web Developer Toolbar – This is another very useful Firefox Add-on that allows you to:
    • Display element attributes in-line with the page you are viewing.
    • View a page’s JavaScript generated HTML.
    • Resize the browser window to preset sizes.
    • Outline different types of elements in the page.
    • Quickly disable, enable, and delete cookies
    • The list goes on…
  6. JQuery – JQuery is a JavaScript library but I also consider it an important tool to make JavaScript programming less painful. It allows you to easily select a DOM element you want to manipulate without typing a whole lot and handles many of the browser idiosyncrasies. Using JQuery’s selectors, you can easily change attributes and chain those changes to together. JQuery also has shortcuts for event handling, effects, AJAX, rich UI components, and anything else that is repetitious, boring, or aggravating to do in plain JavaScript. It takes a little time to learn JQuery but it quickly pays off. While there are many other JavaScript libraries available, I would say JQuery has become the most popular and so does this very scientific poll ;). If JQuery or one of its hundreds of plug-ins don’t do what you need then JQuery will work very well along side some of the other popular JavaScript libraries such as Dojo, Prototype.js, Ext.js, Mootools, and YUI.
  7. Putty – If you need to connect to your web host via Telnet or preferably SSH, Putty is tough to beat for the price.
  8. WinSCP – WinSCP provides SSH file transfer for Windows machines. If you use Windows on your desktop and a Linux host, this is one of the best ways to upload your files.
  9. Notepad++ – If you want a basic, lightweight text editor to do your coding with color syntax highlighting Notepad++ is a great choice. Even if you use a full featured IDE for your development I find it handy to have a good text editor handy. Notepad++ has several plug-ins available as well.
  10. PHPUnit – If you are developing a web application that you think will be around for any length of time then unit testing is a critical time saver. Chances are you already write tests to check if your code is working if you don’t have a user interface built yet so often you are already committing the time to writing tests. Unit tests developed using a unit test framework allow you to make “assertions” regarding the output of each function or small “units” of code. Unit tests stick with your code throughout it’s life cycle and are usually executed before you check-in a new change. When you run your unit tests using the unit test framework’s executable you can generally test just a single class or your entire code base. This will quickly tell you if the code you just wrote is working correctly and if you have inadvertently broke any existing code elsewhere. For current job my team and I code in primarily PHP and JavaScript so we use the PHPUnit and JsUnit frameworks but there are unit testing frameworks available for nearly all popular programming languages including C++, Java, C#, etc.
  11. Selenium – While unit testing covers individual units of code, integration tests cover how everything works together. Selenium accomplishes this by allowing you to build scripts that you can playback to emulate a user’s browser interacting with your application. Like unit tests, you can make assertions that elements in the web interface are working the way they should. This allows you to effectively perform an automated regression test of your application so you can make sure your code changes didn’t break any interactions between the components of the application. Although I personally really like Selenium, there are other good automated test tools such as Watir
  12. Web Application Vulnerability Scanning Software? – This is an area that will strongly depend on your budget. I think vulnerability testing is essential but short of doing a lot manual probing and experimenting, it is going to cost you. Even if you have read the OWASP guide back and forth and are careful to escape all your inputs, I still think it is important to run a test tool against your app before you release it into the wild. Although automated testing won’t reveal all your security issues it will at least reveal some of the more embarrassing ones. If nothing else, it is important that the script kiddies don’t find anything interesting when they do the same. While there are many open source security tools, I am not aware of any that will do automated application security scanning like HP’s WebInspect, IBM’s Rational AppScan, or Acunetix WVS. I found an interesting comparison between the three here. I would be interested in any alternatives if you know of any.
  13. Security Web Sites – The following web sites are good security “tools”:
  14. WebLOAD? – Instead of hoping your web application will hold up to high traffic volumes, wouldn’t you like to really know for sure? A load testing tool is essential to predicting how your application will behave under load and will also help identify bottlenecks in the application that can be optimized. Optimizing your application based on load test data will help to ensure you are focusing your optimization efforts on the real problem areas. In my current position we were using the alleged open source version of WebLOAD, which, worked pretty well. Unfortunately Radview is making new versions, err… I mean, pro, closed source and I am guessing the licensing costs are ridiculous just by the fact that they don’t list the price on their website or provide a shopping cart so you have to contact their sales folks (I am not a big fan of this practice). I am fine with convincing my boss to buy software if need be so long as it doesn’t cost so much he will laugh at me. So… I am in interested to hear about any other load test tools you have experience with.
  15. Subversion – Version control is essential if you are working on a project for any length of time and especially with other people. Subversion is easy to setup and as long as you back up your Subversion server and make frequent commits, your code will be safe and you can always revert to a previous version if you really mess it up. CVS is also an option but I prefer Subversion because it maintains versions across the entire code base instead of just individual files. This allows you to easily revert a bad multi-file commit. There are plenty of clients available for Subversion and many development tools have subversion support built-in or provide plug-in options.

Well that’s it for this post. I would really like to hear about any other must-have web app dev tools that you think should be on the list.

Review: Herbie the Mousebot Kit by Solarbotics

5 out of 5 stars

img_7866

I wanted a quick, easy to build robot kit to get back into electronics and robotics. I ask for a Herbie the Mouse Bot for Christmas and sure enough I got one. It was a fun kit to build and went together in a little over an hour.

You start off with a PC board…
Herbie the Mousebot by Solarbotics

… and a handful of components…

Herbie the Mousebot by Solarbotics

You break apart the PC board which serves as a PC board and a body for the mouse which, is pretty cool.

Herbie the Mousebot by Solarbotics

The PC boards join together via several solder joints. Tape is used to keep everything together until you are done soldering. A smaller board that holds the 9 volt battery connector helps keep the three main sides together. By the time you are done soldering all the joints it is a pretty sturdy little robot.

Herbie the Mousebot by Solarbotics

Herbie the Mousebot by Solarbotics

img_7855

The whiskers and tail activate a relay when bumped so the mouse will backup to avoid getting stuck. I taped the relay down while I soldered it in.

img_7859

Herbie with the photo diode “eyes” installed….

img_7861

Herbie just about finished…

img_7862

img_7866

Herbie is an interesting robot because it uses a simple analog IC, the LM386, to do something you might think requires a much more complicated digital circuit or micro-controller/processor:

http://downloads.solarbotics.net/PDF/Solarbotics_Herbie_the_Mousebot_Instruction.pdf

img_7868

Herbie the Mousebot by Solarbotics

We don’t have much open bare floor in the house and it moves quite fast so it was a bit of a challenge to keep it under control without hitting too much:

The Herbie Kit is well engineered and fun to assemble. I give it five stars and recommend it as a good first robot kit.