PHP File Last Modified and Filesize (bytes to KB/MB)

$file = 'image.jpg';  
  
// File Last Modified Time  
$filetime = filemtime($file); // displays in seconds since the Unix Epoch  
echo date("M j, Y", $filetime); // would display the file creation/modified date as Feb 3, 2007  
  
// File Sizes
$filesize = filesize($file); // displays in bytes  
echo round(($filesize / 1024), 2); // bytes to KB  
echo round(($filesize / 1048576), 2); // bytes to MB  
echo round(($filesize / 1073741824), 2); // bytes to GB  
Continue reading →

MySQL Return Single Data or Single Array with PHP

Quite a few times during projects I only want to find a single value from the database. But rather than setting up WHILE loop, I\'ve been using a quick function that does two things perfectly.

Here\'s the function:

// returns single result
function mysql_one_data($query)
{
   $one=mysql_query($query);
   $r=mysql_fetch_row($one);
   return($r[0]);
}

// returns single array
function mysql_one_array($query)
{
   $one=mysql_query($query) or die (mysql_error());
   $r=mysql_fetch_array($one);
   return($r);
}

Say for example I wanted just the date of a record. All I have to do is make sure to call the function and then do the following:

// single value
$date = mysql_one_data(\"SELECT date FROM records WHERE id=\'2\' LIMIT 1\");

// single array
$date_info = mysql_one_array(\"SELECT firstname, lastname, date FROM records WHERE id=\'2\' LIMIT 1\");

// you can then use the single $date_info array like
echo $date_info[\'firstname\'].\' \'.$date_info[\'lastname\'].\' (\'.$date_info[\'date\'].\')\';
Continue reading →

Compare Two MySQL Tables

The other day I needed to compare two tables and see what data was in one but not in the other.

table_a is my main table that I want to update and table_b is my storage table that I have my updates in. The query below will return all records that are in table_b, but no in table_a. Then you can take those results and use them to INSERT the missing records.

SELECT table_b.* FROM table_b
LEFT JOIN table_a ON table_b.id = table_a.id
WHERE table_a.item_id IS NULL

Update: 11/18/2010
JBES commented on another way to do the same thing. I tried it on two tables with over 4 million records and the difference in performance was very minor. But if you notice any huge difference, please let us know in the comments. (this method is a little cleaner IMHO)

SELECT * FROM tblA
WHERE tblA.ID
NOT IN (
   SELECT tblB.ID
   FROM tblB
   WHERE tblA.ID=tblB.ID
)
Continue reading →

Why God Made Pets

They help out around the house...
dog_helping_in_kitchen.jpg
Continue reading →

Convert ISBN10 to ISBN13 with PHP

Since at the start of 2007 all books will no longer be using the ISBN10, we needed a way to convert all those ISBN10 numbers to the new ISBN13 (or EAN).

Continue reading →