Skip to content

PHP: Iterate through object members

May 24, 2012

I was doing some work on a small project of mine in PHP and decided their must be a more efficient way to sanitize all string members of an object.  Previously, my old method was a blunt procedural style similar to something that would be seen in C.

public function escape($link) {
	$this->name = mysql_real_escape_string($name, $link);
	$this->description = mysql_real_escape_string($description, $link);
	$this->address = mysql_real_escape_string($address, $link);
}

And so forth for every object that would be passed to and from the database.  I’ve been studying up on Ruby and in doing so learned to take a few different angles to coding.  It turns out PHP supports “duck-typing” functionality similar to that in Ruby. Due to the way PHP handles objects and arrays with such similarity, I thought perhaps it would be possible to iterate through members of an object. While I’m not much too exposed to paradigms outside of the traditional object-oriented style and procedural style, a bit of thinking outside my traditional box helped me realize how simple a more graceful solution would be (also note  the new version is using a mysqli object for the connection instead of the mysql_ set).

public function escape($link) {
	foreach($this as $member => $value) {
		if(is_string($value)) {
			$this->{$member} = $link->real_escape_string($value);
		}
	}
}

Pesky Common Sense Database Rules (MySQL)

May 13, 2012

Recently, I’ve found myself iterating over some fairly simple MySQL database designs of a yet still early development application.  However, several times I found myself running in to this same error:

ERROR 1005 (HY000): Can’t create table ‘test.board’ (errno: 150)

The following bit of code is an example of a subtle case of logic sneaking up behind my code and catching me off guard. See if you can spot the problem with the SQL alone.

CREATE TABLE IF NOT EXISTS test.board (
id INT(11) NOT NULL AUTO_INCREMENT,
category_id INT(11) NOT NULL,
creator INT(11) DEFAULT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
name VARCHAR(64) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT fk_board_cat
FOREIGN KEY (category_id) REFERENCES category(id)
ON DELETE SET NULL
ON UPDATE NO ACTION,
CONSTRAINT fk_board_creator
FOREIGN KEY (creator) REFERENCES user(id)
ON DELETE SET NULL
ON UPDATE NO ACTION
)
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci
;

During this overall process I’ve seen the same error crop up in more than one way (note the above SQL does have a problem with it’s logic but not it’s syntax and won’t work in any case).  The error at hand which have somewhat eluded me tonight is MySQL error 1005.

ERROR 1005 (HY000): Can’t create table ‘test.board’ (errno: 150)

Can’t create table? Rather helpful advise there.  Read more…

Oracle 10g R2 Listener Setup

February 21, 2012

ORA-12514: TNS:listener does not currently know of service requested in connect descriptor

This error can be encountered when a dynamically registered database listener has been removed, such as when a database is shutdown. To prevent the listener from losing a database service when it is shutdown, the server must be statically registered with the listener.

Listeners can be configured via Oracle Net Manager [see below image].

Configuring a Listener in Oracle Net Services

Configuring a Listener in Oracle Net Services

7-Zip Context Menu Not Working?

February 10, 2012

I’ve been using 7-Zip for years now and stumbled across a strange issue on my laptop. I’ve been unable to manage to get the context menu to show up despite checking the settings and reconfiguring them various times. It seems, when I downloaded 7-Zip I paid no attention to 64-bit vs. 32-bit, and here in lies the problem. I am running Windows 7 64-bit but had installed the 32-bit version of 7-Zip. Uninstalling the 32-bit version and installing the 64-bit version has solved the issue in what turned out to be a much easier fix than I had anticipated.

The remaining issue, would be to determine what causes the difference in behavior. Maybe something to do when I ever find the time…

Bringing Chrome Up to Snuff

September 26, 2010

Back in the day (okay, last year) when Firefox was my primary browser, I did quite enjoy the peace of mind from using NoScript and WoT. Of course, when I’ve done any web development I’ve also always found Live HTTP Headers and without a doubt, FireBug, invaluable.

Read more…

Windows 7: Run Command Prompt as Admin

September 14, 2010

Something which has been irking me for a while lately has been the fact that command prompt defaults, like all other programs, to running in non-elevated mode. When I want to run command prompt, I’m used to using [Windows] + R and throwing cmd [Enter] without really paying any attention to what I’m doing. Alas this is an annoyance when I need to do something as mundane as netstat -nb.  Of course my first step was to check if I could set the program to run in admin mode in compatibility – to no avail.

After reading more than I think I really should have needed to for something as simple as this, I’ve finally found two ways which are somewhat convenient for accessing command with admin rights.

  • Type the command into the Start menu’s search box and use [Ctrl] + [Shift] + [Enter] to launch the program.
  • Edit a shortcut file to run as an admin. On the shortcut tab’s advanced properties dialog, you have the option to run the specific shortcut as an administrator. Note that if you’re using a shortcut like this you can go as far as even pinning it to your taskbar and the admin setting will continue to work.

As for the time being the shortcut trick is my preferred method of accessing command prompt with admin rights. Of course if anyone wants to share an easier way I’d be glad to hear it…

SQLite on Java

February 12, 2010
tags: ,

I’ve been poking and prodding a pet project of mine for while now which I’ve designed for keeping track of recipes. Problem with it is I’ve written it to depend on my MySQL server (I never thought about sharing it apparently while developing it). So now I’ve decided to try porting the database end to SQLite. Right now, I’m only familiar with MySQL and Oracle.

Anyhow, I’ve stumbled upon SQLiteJDBC, and I think I will be giving this a try for my port. Wish me luck, and hints and tips are welcome!

Follow

Get every new post delivered to your Inbox.