Archive for the ‘experimental scripts’ Category

Use PHP & YQL to find out what city a zip is from

$_GET['zip'] = '16601';
//Validate the zip
$zip = str_replace(" ", "", $_GET['zip']); //remove white-space
if (!ctype_alnum($zip)) {
    die("Enter alpha-numeric numbers only");
if (strlen($zip) > 7) {
    die("Zip query is too long");
$zip = urlencode($zip);
$url = "*%20from%20google.geocoding%20where%20q%3D%22$zip%22&";
$xml = file_get_contents($url);
$location = new SimpleXMLElement($xml);
$status = (string)$location->results->json->Status->code;
if ($status !== '200') {
    die('Could not get location');
$country = (string)$location->results->json->Placemark->AddressDetails->Country->CountryName;
switch ($country) {
case "USA":
case "Canada":
    echo (string)$location->results->json->Placemark->address;

    echo 'false';
Categories: experimental scripts

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