Linux mysql PHP shell

PHP PDO Mysql 101


Needed to make a simple php script with mysql in it, so I decided to use PDO because… well peer pressure 😐 so heres a basic PHP PDO MYsql script.php pdo mysql

The idea is I have a table called table with a field called field in a database called somedatabase. We have 1000 rows of data in said table and we need to pick a random result from the 1000 rows. This is just 1 way of doing it, it isnt the most optimized, neither is it the cleanest. but this will give you a basic idea of how to connect to the DB and get a result. you can expand this of course to do very tricky queries.


// Add a user variable for PDO, mysql user
// Add a password for PDO, mysql password
// Setup the mysql connection info
$dsn = 'mysql:dbname=somedatabase;host=';

// Wrap the actual connection to mysql in try so if it doesn't work you can catch the exception and echo the message.
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();

// Pick a number from 1 - 1000 and assign to $rc
$rc = mt_rand(1, 1000);
// Assign the SQL query selecting the data from our DB / field to variable $sql
$sql = "SELECT field FROM table LIMIT 1 OFFSET $rc";
// run the query and for each one assign to variable $field
foreach ($dbh->query($sql) as $row) {
$field = $row['field'];

// clear the variable $dbh and return used memory back to usable pool
$dbh = null;

I want to write more articles concerning the trio of PHP PDO Mysql but I don’t do much with PHP anymore so the chance that I have need of the three at this point is slim.


PHP Image Magick Drop Shadow

Want to add a simple drop shadow on any image? well here’s how to do it on the fly via PHP and Image Magick.

< ?php $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    header( "Content-Type: image/jpeg" );
    echo $drop_shadow; 

Here’s how to do same via PHP and Image Magick but writing the file out to a second image leaving your original alone.

<?php $filename_shdw = 'images/image_shadow.jpg'; if (!file_exists($filename_shdw)) { $img = new Imagick('images/image.jpg'); $img->setImageFormat('png');
    $shadowed = $img->clone();
    $drop_shadow = $img->clone();
    $drop_shadow->setImageBackgroundColor( new ImagickPixel( 'black' ) );
    $drop_shadow->shadowImage( 80, 3, 5, 5 );
    $drop_shadow->compositeImage( $img, Imagick::COMPOSITE_OVER, 0, 0 );
    $drop_shadow->writeImage( 'images/image_shadow.jpg' );
    echo ''; 

these snippets are very simple, you can expand them, include them in your own code, randomize the drop shadow etc. I will expand this in another post.

PHP regex

php Filter var & common regex patterns

People … quit messing about with ereg_replace / preg_replace / eregi or other things you know nothing of… use the php filter_var. DO IT.
Commonly found bullshit on the interwebs :

function isValidEmail($email){
    return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,3})$",$email);

eregi is deprecated and silly, the above WILL miss some of the weirder or longer email addresses like school/company addresses. make your life simpler :

function isValidEmail($email)    {
    return filter_var($email, FILTER_VALIDATE_EMAIL);

while not the most perfect thing, its easier to remember AND add to your code.

More info regarding this : PHP manual Filter Var

Some examples :

/*** validate bools ***/
echo filter_var("true", FILTER_VALIDATE_BOOLEAN);

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
        /*** no ***/
        echo "Sorry, $url is not valid!";
        /*** yes ***/
        echo "The URL, $url is valid!