Importing data to MySQL 0

This is more of a note-to-myself entry to dig it up later easily, but the syntax to import dump file to MySQL is:

mysql -p -h database_server -u mysql_user mysql_dbname < file_dbname.sql

Good resource with more on this is here.

Speaking of importing to MySQL, I was unable to find a ready solution to import JSON data to MySQL, so I just wrote a quick script using PHP json_decode() function which is available after PHP version 5.2.0 and PECL version 1.2.0. The script is not here as I did not make it customized (though if any of you will require, I will post an example here), but the function should be a good enough hint.

Javascript relation: Parent to iFrame 0

This goes out for my own future reference as well as for those people who are not that fluent in Javascript and are looking for this simple piece of information. I am saying this as I spent great deal of time locating it on Google, but remained unsuccessful. Internet is full of examples about the opposite way of affection: iFrame to parent, but not the way round.

The “secret” is simple:

As you do in the iFrame->Parent relation:

parent.document.*

Example:
parent.document.getElementById(’elementName’).style.display=’none’;

the  Parent->iFrame goes similarly like this:

window.frames['frameName'].document.*

Example:
window.frames['fr'].document.getElementById(’el’).style.display=’none’;

Where frameName is the iFrame name specified in “name” attribute of the iFrame.

HTC S740 - the next best phone in the world 0

Now for about 2,5 years I had been using my good old HTC S620 phone (which still looks brand new by the way), and for all this time I considered it to be the best phone in the world as it completely met my needs and wants. Naturally, having the best phone in the world, I did not care about all the inferior phonemodels, cell phone market trends and cool new functions in the cell phones. However, as the time passed I decided I needed a new phone. When the decision came (about half year ago), I was hoping to get something cool as I got my first best phone in the world over 2 years ago, and 2 years for technology is sort of a light-year. And with “something cool” I mean:

  1. (no doubt) built-in TV with “anytime-anywhere” access to all movies ever made projected on the wall with the built-in projector
  2. personal assistant that appears as a 3D projection of human head, with which I can interact using normal language in speech
  3. the phone can be used as flash-light, shaver, umbrella, cooler for the beer, credit card, hockey puck or stick, swimming shorts and towel, and of course bottle opener, and with all that still fit in your palm
  4. I was hoping for some kind of hover-phone which would stop like 10cm above the ground if dropped, but I got the feeling from the general news that human race is not there yet as with teleporting
  5. for the very least I expected that the cell phone can be turned in a power-shield and that light-saber thing from Star Wars
  6. forget about charging - the phone can find means to charge itself

But to my huge disappointment - NO! NONE of these things mentioned! None! Not even the light-saber.. except phones seem to have got close to implementing portable projectors in them.

Read more »

Javascript vs. women 0

I am relatively new to Javascript, but I have come to one conclusion already: Javascript is like women - you quickly realize there is some problem with it, but it takes unreasonable amount of time and effort to locate the problem.

Though there is also one major difference - Javascript problems get solved fast once located..

Get query string variables of Javascript files 0

Looking at different scripts linking to Javascript and AJAX frameworks such as Scriptaculous, I noticed that it allows loading of particular parts of it by adding them in the following query string such as this:

<script type="text/javascript" src="scriptaculous.js?load=effects,builder"></script>

Looking at scriptaculous.js itself, it relied on some built-in functionality for that. I was unsuccessful in finding online a ready function to do exactly that, so inspired by this piece of code and borrowing the regex part of it, I wrote a function which grabs the additional parameters and allows using them in the Javascript:

function getJSvars(script_name, var_name, if_empty) {

var script_elements = document.getElementsByTagName(’script’);

if(if_empty == null) {
var if_empty = ;
}

for (a = 0; a < script_elements.length; a++) {
   var source_string = script_elements[a].src;
       if(source_string.indexOf(script_name)>=0) {

       var_name = var_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
       var regex_string = new RegExp("[\\?&]"+var_name+"=([^&#]*)");
       var parsed_vars = regex_string.exec(source_string);
       if(parsed_vars == null) { return if_empty; }
       else { return parsed_vars[1]; }

      }
   }
}

Usage of it is simple: if you have a Javascript which should parse some dynamic variables (such as user or account ID), you can include it right after the included Javascript like this:

<script type="text/javascript" src="script.js?var1=value1&var2=value2"></script>

instead of creating a server-side generated dynamic Javascript using PHP or other language. The values of variables like var1 can be retrieved like this:

var var1 = getJSvars(’script.js’, ‘var1′);

You can also add a value as third parameter to output in case var1 is empty, but it is optional.

Now this is scary 0

Could you possibly imagine it is this easy to capture what is typed on your keyboard? Wirelessly.. even through a wall!

Read more »

Performance: PHP4 vs. PHP5 0

I was doing optimization of one high-traffic PHP application, which had around 2 million page-views and 20 million MySQL queries per day, and was running on a dedicated Linux server. I listed all the possible sources of high CPU consumption and server load which could be optimized. One of the possibilities was upgrading PHP version as the application was running on PHP4 due to incurred problems with PHP5 when it was previously running on shared hosting. Therefore it used PHP5 only for features that relied on the new functions of it. Also, I had read general conclusions online that performance of PHP5 is better than of PHP4, so I had to look into this. Read more »

Welcome! 0

In this web site you will find different useful on-line tools, code snippets, maybe even different ideas and thoughts on different topics. The purpose of this web site is to serve more like a personal notepad, but it is aimed also to be useful for others.