Archive for September, 2010

filter_input() return value, an undesired result

September 27, 2010 Leave a comment

I love PHP built in filter functions… but in this use case, they always trick me:

I ran this on the command line:

$_GET['name'] = 32;

var_dump(filter_input(INPUT_GET, "name", FILTER_VALIDATE_INT));

I got null, when I should get (int) 32.

However, if I type in a browser, say the page is test.php … test.php?name = 32

and run the code

var_dump(filter_input(INPUT_GET, "name", FILTER_VALIDATE_INT));

I get the right answer (int) 32.

Evidently the built in filter functions treat $_GET[‘name’] differently in the script, than when it is actually passed over $_GET…

Go figure.

Categories: PHP News

Notes: Rendering on IE 8

September 21, 2010 Leave a comment

IE 6 – Aug 27, 2001
IE 7 – Oct 2006
IE 8 – March 19, 2009
IE 9 (beta) – Sept 15th 2010

IE 8 rendering modes:

IE 8 has stricter adherence to W3C than IE 7.

Pages coded to the quirks of IE7  break in IE8 because IE8 is closer to W3C standards.

To maintain backward compatibility between IE7 and IE8, MS came up with this solution:
<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7″ />, a meta element that tells IE to emulate ie7.
IE8 can also be told to emulate IE7 in the headers.

We also have such greats as:
<meta http-equiv=”X-UA-Compatible” content=”IE=8″ /> always render in IE 8

<meta http-equiv=”X-UA-Compatible” content=”IE=Edge” /> use the most current engine

IE8 has a black list of sites to render as IE7. (Users can opt out)

Coincidently, if a there is no doc type specified, IE 8 renders as IE 5.5, or quirks mode.

This post goes into a deeper and better explanation of the whole phenomenon. This is where I learned most about it:

As a side note, the part in Faruk Ateş article that I found most interesting is that if your website is viewed through a black listed frame, such as the digg bar or google images, it will render in IE7 mode, and vice versa.

If there is no doc type but “X-UA-Compatible” is specified, the browser will still stay in quirks mode.

Categories: CSS

How to specify specific style sheets for IE6, IE7

September 21, 2010 Leave a comment

<link rel="stylesheet" type="text/css" href="main.css">

<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="ie6.css"><![endif]-->

<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="ie7.css"><![endif]-->

Put in the document head AFTER  main style sheet. These conditional statements are proprietary to IE.

Categories: CSS

experiment – Adding images to Sqlite database with PDO, then display an image

September 10, 2010 Leave a comment

The best resource on using PDO is Wez Furlong’s talk available here

I used slide #43, and a further comment by Wez Furlong here to hack together this script.

Now, a little on this script. You’re not going to want to use this code on a production server. This script is the bare minimum it took me to put an image into an sqlite database, and then to display it. It didn’t work exactly as I wanted. I wanted to add a picture then display it. This script adds a picture, then displays an image, but you have to specify the id. <– my test image <– test sqlite database

$dbh = new PDO('sqlite:images.sqlite');
$stmt = $dbh->prepare("insert into images (id, contenttype, imagedata, name) values (?,?,?,?)");
$fp = file_get_contents('test.png');
$type = '.png';
$name = 'test.png';
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $type);
$stmt->bindParam(3, $fp);
$stmt->bindParam(4, $name);
/* $id = $dbh->lastInsertId()); using ->lastInsertId() to get the last ID did not work for me. If it did this script would display the image you just added */
$id = 1; //show the first image
$query = $dbh->query("SELECT imagedata from images where id = $id");
header("Content-Type: image/png");
echo $query->fetchColumn();

Categories: experimental scripts

PDO->exec VS. PDO->query

September 7, 2010 1 comment

PDO->exec VS. PDO->query

These functions are for direct execution of SQL. You probably shouldn’t use them. I advocate the use of prepared statements instead:

Prepared statements automatically escape your input

You compile a pepared statement once, and execute as many times as you like

Nevertheless, PDO->exec and PDO->query do exist. So what are they good for ? What’s the difference between the two ?

Well, let’s talk about escaping first.
First, before you input any data into your database using PDO->exec() or PDO->query() you need to escape your data using the PDO->quote() method.

The PDO->quote method will escape your data using the database specific driver for your data base. Now let’s quote something..

$dbh = new PDO('sqlite:RandomData.sqlite');
$badinput = "Ain't no shame in my game, man";
echo $dbh->quote($badinput);
//'Ain''t no shame in my game, man'

So what is PDO->exec?  PDO->exec just inserts, updates or deletes. It only returns the number of rows affected or false.


$dbh = new PDO('sqlite:RandomData.sqlite');
//lets see pdo exec in action
$data = $dbh->quote('aint no shame in my game');
$sql = $dbh->exec("INSERT INTO junk (data) VALUES ($data)");
var_dump($sql); //returns int(1)

PDO->query() is more versatile than PDO->exec.
Here we substitute PDO->exec with PDO->query. We got the # of rows affected by calling the PDOStatement->rowCount() method

$dbh = new PDO('sqlite:RandomData.sqlite');
$data = $dbh->quote('aint no shame in my game');
$sql = $dbh->query("INSERT INTO junk (data) VALUES ($data)");
var_dump($sql); //Returns PDOStatement object(PDOStatement)#1 (1) {
 //string(59) "INSERT INTO junk (data) VALUES ('aint no shame in my game')"
echo $sql->rowCount(); //returns the # of rows affected

The PDOStatement->rowCount() method returns the # of rows that inserted, updated, or deleted… but it’s fickle on returning the # of rows from a select statement. The ability to return the # of rows from a select statement depends on the database.

A fool proof method of getting the number of rows from a select statement in PDO is to just use count() on the array you are returned.

Here is an example

$dbh = new PDO('sqlite:RandomData.sqlite');
$data = $dbh->query('Select * FROM junk');
$result = $data->fetchAll(PDO::FETCH_ASSOC);
echo count($result);

Categories: PDO

Ancient Greeks – I think they would prefer wide screen

September 6, 2010 Leave a comment

Is it a coincidence that the wide screen formats on the link above have an aspect ratio of 1.6 while the golden mean has a ratio of 1.61803399 ? It looks to me like if you were going to buy a dvd with an ancient greek he’d get the widescreen version.

Wikipedia link to the golden ratio

Standard ratio is 1.33

Categories: Uncategorized

The use of foreach with the PDOStatement class

September 6, 2010 Leave a comment

First, a little on the Traversable interface…

The traversible interface has no constants, static properites, static methods, properties or methods.

Interface [ <internal:Core> interface Traversable ] {

- Constants [0] {

- Static properties [0] {

- Static methods [0] {

- Properties [0] {

- Methods [0] {

You can’t implement the traversable interface either. If you try to implement the traversable interface you get this error:

Class test must implement interface Traversable as part of either Iterator or IteratorAggregate in Unknown on line 0

So what is the Traversable interface good for? If something implements the Traversable interface, you can use foreach() on it.

The PDOStatement class implements the Traversible Interface,

Here are the two ways I know of that you can use to foreach() your PDOStatement object.

Number 1.

In the example below “$sqlite” is a PDOStatement object returned by $dbh->query. You can use foreach to iterate over the results

I’m using this database:

//Connect to the database
$dbh = new PDO('sqlite:RandomData.sqlite');
//$sqlite is a PDOStatement object, and the query is executed with $dbh->query
$sqlite = $dbh->query("SELECT * FROM junk");
//iterate over the values
foreach($sqlite as $value) {

Number 2.


//Connect to the database
$dbh = new PDO('sqlite:RandomData.sqlite');

//Our PDOStatement object is returned to us when we call $dbh->prepare.

$sqlite = $dbh->prepare("SELECT * FROM junk");

//Next we call $dqlite->execute(); which actually executes the prepared statement.

//Now that the prepared statement is executed, our results are ready to be iterated over
foreach($sqlite as $value) {
//If I did not call $sqlite->execute() the foreach call would not have worked, because the prepared statement would not have been executed.

Categories: PDO