Archive

Archive for the ‘experimental scripts’ Category

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


<?php
$_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 = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.geocoding%20where%20q%3D%22$zip%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
$xml = file_get_contents($url);
$location = new SimpleXMLElement($xml);
//var_dump($xml);
//var_dump($location);
$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;
    break;

default:
    echo 'false';
}
Advertisements
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

http://devzone.zend.com/article/4765-The-ZendCon-Sessions-Episode-21-PDO-PHP-Data-Objects

I used slide #43, and a further comment by Wez Furlong here http://www.launchcode.co.uk/archives/37-PDO-SQLite-support-just-doesnt-work-fully.html 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.

http://php-blog.s3.amazonaws.com/test.png <– my test image

http://php-blog.s3.amazonaws.com/images.sqlite <– test sqlite database


<?php
$dbh = new PDO('sqlite:images.sqlite');
//$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$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);
$stmt->execute();
/* $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