About jontas

I like to make things that people find useful.

MYSQL Creating several UUIDs at one single time

I recently ran into the problem that I had too add two rows to a database that uses a UUID (in this case converted to a binary) as a key.
The problem I had was that running two insert statements after each other (just separated by “;”) then the same id was generated.
A workaround I have found is to sleep for a second between the two statements.

Examples (using SELECT instead of INSERT):
Getting the same id twice

SELECT UNHEX(REPLACE(UUID(),'-','')) AS uuid1,  UNHEX(REPLACE(UUID(),'-','')) AS uuid2;
-- Shows: "849392548f1311df91b70019dbd264f1"	"849392908f1311df91b70019dbd264f1"

Adding a sleep to get different ids

SELECT UNHEX(REPLACE(UUID(),'-','')) AS uuid1, SLEEP(1), UNHEX(REPLACE(UUID(),'-','')) AS uuid2
-- Shows: "3901c1928f1511df91b70019dbd264f1"	"0"	"399a5b148f1511df91b70019dbd264f1"

And another one using INSERTS

INSERT INTO testTable (id) VALUES (UNHEX(REPLACE(UUID(),'-','')));
SLEEP(1);
INSERT INTO testTable (id) VALUES (UNHEX(REPLACE(UUID(),'-','')));

PHP: Converting a bool value to a string value

The first idea that comes into mind for doing this is to simply make a typecast of the boolean variable to a string and use that (which works)

$testVar = false;
echo (string)$testVar; //will output a empty string (as that evaluates to false)
$testVar = true;
echo (string)$testVar; //will output 1 (as that evaluates to true)

but what if the requirement is that the string should say true or false

$testVar = false;
echo $testVar ? 'true' : 'false'; //will output false
$testVar = true;
echo $testVar ? 'true' : 'false'; //will output true

(While the above example might seam useless I recently had to do this for a db insert)

Disable (or change) the Click sound in Internet Explorer or Windows Explorer

When you open a folder in Windows Explorer a click sound is played. To disable this there are two ways to do it:

  • Turn it off using the control panel:
    1. Open “Start”->”Settings”->”Control Panel”
    2. Open up “Sounds and Audio Devices”
    3. Open the tab that says “Sounds”
    4. Scroll down until you find “Windows Explorer”->”Start Navigation”
    5. When you mark this line at the bottom there is a drop down that is labeled “Sounds:” Here you should be able to change to anther sound or “none” if you do not wish a click.
  • The second option is a bit more direct and rude – but if the above does not work for you (for me something kept resetting the old sound); just remove the file that contains the click sound.
    1. Open up a Windows Explorer and go to “%windir%\Media” and find the file that is named “Windows XP Start.wav”
    2. Either remove it (permanently) or rename it to something that windows won’t recognize (but you can – so you later can reverse this if you would like to)

Different line-endings on diffrent OS

Windows, Linux/Unix and Mac have different line-endings on text files.

A quick overview on the endings are:

  • Windows-style line endings are CRLF ( \r\n or hex 0D0A )
  • Mac-style line endings are CR ( \r or hex 0D )
  • Unix-style line endings are LF ( \n or hex 0A )

A HEX editor can be used to double check that the correct endings are written.

Pine: Flags

To mark a message as unread in pine this can be done using flags.
First flags have to be turned on in setup
[M]ain [S]etup [C]onfig
Then find “enable-flag-cmd” and turn it on.
After this marking a message as unread is done by “* N” in the list view or when looking at the message.

All the flags are:

  • [N]New
  • [D]Deleted
  • [*] Important
  • [A] Answered

[^T] Can be used to see all the flags and set/unset them one by one (or all).

PHP: Static functions in a Class

Static functions can be used without instantiating the object.

A quick example of a static function is shown below as well as a comparison with a non static function.

A quick example of this (in PHP5):

class TestClass{
	static function staticFunction(){
		echo "static function called";
	}
 
	function nonStaticFunction(){
		echo "nonstatic function was called";
	}
}
 
TestClass::staticFunction(); //will echo static function called
 
$testClass = new TestClass();
$testClass.nonStaticFunction(); //will echo nonstatic function was called
unset($testClass);

PHP: converting a uuid stored as BINARY(16) in mysql to RFC 4122 format

If you are storing uuid’s are BINARY(16) in the database, then the following will turn the stored id to the “original” format.

$uuidReadable = unpack("h*",$uuid);
$uuidReadable = preg_replace("/([0-9a-f]{8})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{4})([0-9a-f]{12})/", "$1-$2-$3-$4-$5", $uuidReadable);
$uuidReadable = array_merge($uuidReadable);
echo "uuid is " . $uuidReadable[0];

MySQL inserting UUID to a binary(16) field

It is advantageous to store a UUID (acc to RFC 4122) in a binary(16) field in the database.
It is not hard to create such a table and to insert data into it.

CREATE TABLE IF NOT EXISTS test_table (
  id BINARY(16) NOT NULL,
  name VARCHAR(128) NOT NULL,
  PRIMARY KEY  (id)
) 
 
INSERT INTO test_table (id, name) VALUES
(UNHEX(REPLACE(UUID(),'-','')), 'test1'),
(UNHEX(REPLACE(UUID(),'-','')), 'test2'),
(UNHEX(REPLACE(UUID(),'-','')), 'test3')

apt-get WARNING: The following packages cannot be authenticated!

apt-get is verifying the packages before installing them.
If the keys are not up to date, then apt-get upgrade will issue a warning.

WARNING: The following packages cannot be authenticated!
  ure uno-libs3
Install these packages without verification [y/N]?

The way to solve this is rather simple, just run apt-get update and it should download the keys automatically.

apt-key handles keys, using apt-key list will show you the keys that are on the computer.