Tag Archives: PHP

PHP Alternatives for Nested IF Statements

We’ve all done it. It starts as a single IF statement:

if (empty($var)){ // does it exist?
   $error = 'No username entered';
   return $error;
}
// everything is OK

Then we decide, we need to check something else, so we just add in one more IF statement:

if (empty($var)){ // does it exist?
   $error = 'No username entered';
   return $error;
   if (strlen($var) > 20){ // Is it too long?
      return 'Invalid username';
   }
}
// everything is OK

Continue reading

Install Apache, MySQL, PHP5, and PHPMyAdmin on Debian “Jessie”

This is a quick guide to getting an Amazon EC2 server up and running with LAMP and PHPMyAdmin based on the LTS (Long Term Support) Debian version 8.5 “Jessie”. This guide was written using the Community AMI image debian-jessie-amd64-hvm-2016-04-03-ebs for Debian “Jessie” and assumes you are able to launch the server and connect via SSH.

Amazon assumes an EBS volume of 8GB (t2.nano) or 10GB (t2.micro), but if the server will be storing very little, the real minimum size of the EBS can be calculated safely as:
1.5GB + (RAM x 2)
That is roughly 3GB EBS for t2.nano, and 4GB EBS for t2.micro.

Continue reading

Using AutoHotKey and Ditto with PHP

I’m a purist I suppose. I write PHP, Javascript, CSS, and HTML in Notepad++. I don’t use an IDE (such as NetBeans), but I do have some shortcuts to make things go faster for me. The first one, Ditto, is a clipboard program that remembers the last 10 things put into the clipboard. This means you don’t have to worry about losing that copy/paste because it was overwritten. It’s simple, open, and a no-brainer addition. For those on OSX, Jumpcut is a great option.

The second one is AutoHotKey, which is crazy powerful, but for this example, I’m using its simple text replacement features. For instance, when I type qwhi, it is replaced with this:

while ($row = mysqli_fetch_assoc($result)){
 
}mysqli_free_result($result);

You can imagine how handy this can be. Below are some of the shortcodes I use in my AutoHotKey file. Enjoy.
Continue reading

Mysqli and BLOB binary database fields

This post exists to save you hours of research on the web. The following things may be true about you:

  • You recently transitioned from mysql PHP functions to mysqli functions (staying current)
  • You prefer procedural PHP over object-oriented PHP (functions and arrays are cooler than classes)
  • You are storing some data in a MySQL BLOB, MEDIUMBLOB, or LONGBLOB field, which is Binary

Continue reading

mysqli_result() function to match mysql_result()

This code is licensed under Creative Commons 0 (Public Domain)

As of PHP 5.5, the MySQL functions are deprecated and are removed in PHP 7. The recommendation is to switch to MySQLi functions, which conveniently offer both a procedural (my preference) and an object-oriented structure. Luckily, it’s a rather easy transition to MySQLi as functions names and functions are nearly identical (just change mysql_ to mysqli_). The only exception to this is the mysql_result() function, which has no analog.

Quick review: mysql_result() is used to write less code when your database query is returning only a single row (LIMIT 1) and/or a single column.

$output = mysql_result($result,0);

Pretty simple and straightforward. To replicate this in MySQLi, the following function can be used:

function mysqli_result($res,$row=0,$col=0){ 
    $numrows = mysqli_num_rows($res); 
    if ($numrows && $row <= ($numrows-1) && $row >=0){
        mysqli_data_seek($res,$row);
        $resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
        if (isset($resrow[$col])){
            return $resrow[$col];
        }
    }
    return false;
}

It has one improvement over mysql_result(), which is you can choose to only include the resource and no row and/or column. It will just assume mysqli_result($resource,0,0).

EDITED Dec 19, 2014
Function updated to allow for the column to be referenced by field name as well as numerical offset.

Tips for PHP Developers on the PayPal API

I recently had a large amount of frustration and work trying to get an integration going with the PayPal Express Checkout for Digital Goods API. The steps to just get an account were tricky, but then weeding through all of the API documentation was hard. Mind you, I had just come off of building integrations with both SendGrid and Twilio, which have fantastic RESTful APIs. In all, the entire process was 8 hours. After that much trouble, I wanted to put online a few of the tips, tricks, code, and documentation links to help others out.
Continue reading