PHP list quick tip

Posted on April 14th, 2010 at 6:32 pm

It often happens that i use some function or method that returns an array of elements, but i need just the first one. Assigning this first element to variable is usually done in two lines, for example:

$arr = explode(".", "zxc.vbn.asd");
$first = $arr[0];
// $first = "zxc"

With the help of function list() it can be narrowed down to just one line:

list($first) = explode(".", "zxc.vbn.asd");
// $first = "zxc"

In my opinion that’s a neat solution for common problem, saves just a one line (and some memory), yet judging by the number of times we do “such thing” in the project i think you will find this helpful.

Big MySQL dump import

Posted on December 7th, 2008 at 1:09 pm

Recently i faced problem “How to upload over 100 MB mysql dump into database”. Obviously it cannot be done using phpMyAdmin, because file is to big, i could open the file and execute query after query, but it would take a lot of time, besides opening such a file is a problem by itself. So i started to search for a solution and i found wonderful solution. Read the rest of this entry »

MySQL Select NULL

Posted on October 23rd, 2008 at 5:15 pm

How to select rows from MySQL table where certain field is empty is a problem i stuggled against for a while like … 10 minutes or so, while solution is quite simple. Remember that it will work with MySQL this solution was not tested with other databases and as far as i am concerned it does not work with Sybase.

SELECT * FROM `table` WHERE `field1` IS NULL

There is also variations for this:

SELECT * FROM `table` WHERE ISNULL(`field1`)

While browsing MySQL documentation i found one more interesting function IFNULL(expr1, expr2). What it basically do is: if expr1 is NULL then it returns expr2 else it returns expr1. Here is a sample usage:

mysql> SELECT IFNULL(1,0);
-> 1
mysql> SELECT IFNULL(NULL,10);
-> 10
mysql> SELECT IFNULL(1/0,10);
-> 10
mysql> SELECT IFNULL(1/0,'yes');
-> 'yes'

That’s it for now, i hope you will find it helpful.

Shorthand for MySQL JOIN

Posted on September 5th, 2008 at 9:08 pm

Hey guys, i am currently finishing my degree at collage and do not have a lot of time to write as you can see. Last month was short on post and i believe that this month will also be short, although i have already long list of topics i want to write about. Anyway i managed to find some time so here something interesting i found recently. Read the rest of this entry »