My Blog

Blog


Adding New Array Key in PHP

4.02.2010 | 0 Comments

In adding a new array key, we’ll need to know two array functions called array_key_exists and array_merge.

First, let’s take a look at array_keys. It’s a function that returns an array containing the keys of an array.

<?php
$a = array("foo"=>"1","bar" => "2");
$keys = array_keys($a);

print_r($keys);
?>

Second, of course, is array_merge. As the name implies, it just merge the two array.

<?php
$a = array("foo" => "1", "bar" = "2");
$b = array("name" => "Juan", "gender" => "male");
$c = array_merge($b, $a);

print_r($c);
?>

So here’s the final code we have.

<?php
$a = array("foo" => "1", "bar" = "2");
// keys to be added
$b =array("name" => "Juan");
$c =array("gender" => "male");

$a = array_merge($a, $b, $c);

print_r($a);

?>

The code above is too simplistic ( we didn’t even use the array_keys function) but if you are not certain with the keys you of the array to be merged, then this functions become handy. Example as below.

<?php
$a = array("foo" => "1", "bar" = "2");
/* let's say $b comes from a function that returns
array with (i.e, array("name" => "Juan", "gender" => "male"))
*/
$b = my_function_ret_array();

$keys = array_keys($b);
foreach($keys as $key){
if(array_key_exists($key, $a){
$a[$key] = $b[$key];
}
else {
$a = array_merge($a, array($key => $b[$key]));
}
}

print_r($a);

?>

Again, I must remind my readers that there might be better way of doing this. It all depends on the situation and for the time being, this is the functions I used. If you want to suggest a new one, please share it in the comment box.


Get Category Name in WordPress

2.03.2010 | 0 Comments

This is another super simple and yet super helpful WordPress function that took me a while to find. I’m currently working on a WP Theme that requires me to show the category name. And I thought it isn’t right to write an SQL including all the necessary stuffs that comes with querying the database just to get the category name. There must be a simpler and better way… and there is! Here’s the simple code to do just that.


<?php get_cat_name( $cat_id ); ?>

Cool!

Reference: http://codex.wordpress.org/Function_Reference/get_cat_name


Enable/Disable Bind Variable in SQL*Plus

1.18.2010 | 0 Comments

As state in the title, this post is about turning off the bind variable in SQL*Plus. Actually, this is also applicable in PL/SQL Developer and Toad but I’ll be discussing more on SQL*Plus. You see, I have a problem wherein whenever I load my backup data (saved as insert statements) and there are ampersand (&) or colon(:) somewhere in the script, SQL*Plus will ask me for an input. Of course, I don’t need that because as a backup, I want to load the data as it is. And when migrating 60MB or more, then it isn’t funny to update the query one by one. The good thing is, I don’t actually need to update the query because there’s an answer to my problem. And it’s pretty damn simple.

Syntax


set define off

Oh yes, that’s just one line that will solve my misery. Well, it’s all about my ignorance. To turn back on, then do the opposite as below.

Syntax


set define on


Oracle NVL Equivalent in MySQL

12.28.2009 | 0 Comments

Here’s the MySQL equivalent of Oracle’s NVL and MSSQL/Sybase’s ISNULL functions.

Syntax


IFNULL(expr1,expr2)

Usage

If expr1 is not NULL, IFNULL() returns expr1; otherwise it returns expr2. Take note that IFNULL() returns a numeric or string value, depending on the context in which it is used. I haven’t used it for other data types yet but I reckon it would have the same results. That is to return the same data type as that of expr1. Another important thing to remember is that expr1 and expr2 should have the same data type


MySQL SEQUENCE Equivalent

12.20.2009 | 0 Comments

Most Oracle guys (and I heard PostgreSQL, too) will be missing sequence in MySQL. The nearest equivalent that MySQL has for this feature is the AUTO INCREMENT field. However, auto increment is sometimes not sufficient and time will come that you will be missing it.

Case in point is the alpha-numeric auto-generated code. In Oracle, it’s easy to generate AA123, AA124, AA125, etc., etc. All we have to do is concat the alpha part (AA) and with the nextval of sequence. For MySQL, the alpha part and the numeric part have to be saved in separate fields.

However, Maresa of Microshell has created an alternative solution for it. The article discusses a way to emulate the nextval function in mysql. It a nutshell, they created a table similar to the all_sequences of Oracle. This is where you insert your sequences. Then, create a function named nextval() function which increments and returns value of the sequence.
You can check the procedure here.

It’s pretty neat, actually. It’s an excellent alternative and you can even create similar functions like currval(), prevval(), among others. However, I have some reservations on his approach especially on the issue of concurrency. You see in Oracle, calling the nextval() will increment the value and that’s already persisted (saved) in the sequence table even without saving your query/dml statement. On this approach, other queries accessing the pseudo-nextval will have to wait until your query finishes. Not a good idea.

So for now, I’ll will still have to make do with whatever MySQL is providing. If there’s any consolation, MySQL provides LAST_INSERT_ID() function in case you want to get the latest id inserted. See details here here.

So I’m just hoping that MySQL will implement an equivalent feature in the near future. With the imminent takeover by Oracle, I’m actually hoping that development of MySQL will push through and take a steam. We all know that the development of this widely used open-source database engine has been hampered ever since Sun bought it.

Sequence is a little feature. But little as it seems, it solves a lot of problems and helps improve code development time.