Search
Categories
Archive

You are currently browsing the Top Web Guy .com blog archives for April, 2010.

Archive for April, 2010

A Consistent microtime() Function in PHP

Wednesday, April 21st, 2010

OK. By definition, in PHP,  microtime() should return either micorseconds time and seconds  (Unix Epoch in seconds) in this format:
0.999999999 99999999999
example: 0.478258723 1271831244
or, if true argument specified ( microtime(true) ), the Unix Epoch time combined with the microseconds as one float number. Like this:
1271831244.478258723

The second one is useful for things like timing and performance profiling (subtract two measured instances of the microtime to see time lapse between them).

One problem is that the version of the function with specifed true parameter only works under version 5 and on of PHP.

Here is one way to write portable code that will always give you absolute time regardles of the PHP  version:

function truemicrotime(){
    $x= microtime(true);
    $x = trim($x);
    if (strlen(strstr($x," "))) {
        $a = split(" ",$x); $x = $a[1] + $a[0];
    };
    return $x;
} 

You can use as:

$time_a = truemicrotime();
// some processing, may be sql call
$time_b = truemicrotime();
$time_it_took = $time_b - $time_a;

FIRST and LAST with GROUP BY in MySQL – A Working Solution

Saturday, April 17th, 2010

Ok, so it was my turn to learn that in MySQL there is no implementation of the FIRST and LAST aggregation functions.

I looked up several places on the internet where people talk about this solution but I cols not find a good direct solution.

There was one recommendation to use a SELECT and a LIMIT 1 wit the proper ORDER BY but I did not see an example.

So when I found a solution for myself I decided to publish it here.

Please comment on issues, concerns, ideas.

Basically I was trying to draw a result list to populate a new table from an existing one. I needed aggregated info, grouped by the field (column) ‘word’, where I wanted the first and last update time-stamp of records with the same value of ‘word’ and the first and last ‘ip_address’ that was tracked for the first and last update. I had the ‘updated’ time-stamp of the record and the ‘ip_address’ in the original table and I wanted a result list, one row per ‘word’, the count of records for that word, first and last update and the ip_address for the first and last update. The first and last time-stamps were easy because I could just say MIN and MAX for them since the values is scalar in the ‘updated’ field, but for the first and last ip_address I had to look for the ip_address values in the first and last record per group ordered by ‘updated’

In other SQL implementations one would have done this and it would have worked:

SELECT word, COUNT(word), MIN(updated), MAX(updated), FIRST(updated), LAST(updated)
FROM article GROUP BY word.

In some languages you had to specify ORDER BY in the FIRST and LAST parenthesis like this:

SELECT word, COUNT(word), MIN(updated), MAX(updated),  
FIRST(updated ORDER BY updated), LAST(updated ORDER BY updated)
FROM article GROUP BY word.

In some you just have to rely on the proper index used possibly by using the WITH INDEX technique.

Here is what I did in MySQL to get what I wanted:

SELECT
word , COUNT(word) ,  MIN(updated) , MAX(updated) , 
(SELECT a.ip_addr FROM article a
WHERE a.word = article.word
ORDER BY a.updated  LIMIT 1) AS first_ip
,
(SELECT a.ip_addr FROM article a
WHERE a.word = article.word
ORDER BY a.updated DESC LIMIT 1) AS last_ip
FROM notfound 
GROUP BY word;

It worked perfectly and I do not think there is any performance issues. The query over 200,000 records including the update / insert of a new table (of 20,000 aggregated records) took less than a 100th of the second.

The sub-queries used in the query use indexes on ‘updated’.

A Working Solution To: “Dreamweaver cannot use the prefix you entered to display live data. Please double check …”

Monday, April 12th, 2010

There are a lot fo postings on the internent forums about a problem people experience with trying to create a new website in Dreamweaver to work with a dynamic serverside technology like PHP/MySQL or ASP.NET or ColdFusion.

When you get to a point to specify the URL of your website, which should be in the form: http://mydomain/mywebiste/, if you happen to be wirking with a brand new website (that by the way may be perfectly well setup) you may get the followig error from Dreamweaver:

“Dreamweaver cannot use the prefix you entered to display live data. Please double-check your site configuration or click on Help for more information on how to correct this problem. (HTTP Error: 12029)”.

Dreamweaver calls the URL you were trying to specify a prefix becuase it intends to correcly use in front of every web page that you are going to have on your website, or in the most complex case – a subdirectory and then page name.

The problem will be experienced especially wiht brand new websites. Again the websites might have been configured perfectly and they may local (on your workstation, PC, Mac) or remote websites.

A working solution:

I was looking for a solution for this problem that I experienced myself.

The text from the error Dreamweaver throws is allover the internet with no practical solution. The most I have found is – “well Dreamweaver needs to be configured” and stuff like that. I played quite a bit with this since I wanted to resolve it. I have webmaster and web development experience now 12 years but never worked with DW. I always used non WYSIWYG editors and I thought I will try the widely admired Dreamweaver. So I wanted to start on the right foot and figure out the problem.

This is what I did.

I was starting a new website on which I had perfectly well installed PHP and MySQL as with the dozens ones I installed before and I even wrote a few short scripts involving both PHP and MySQL to test the HTTP / PHP / MySQL / Apache stack is working fine.

This did not help Dreamweaver identify that there is a working website installed.

Then, though this was the site I wanted to connect with I tried direct DW to one of my existing sites – and that worked – it identified them. “OK, so what is the difference?”, I thought.
Well, one difference I could think of was that all other sides had a default page already set up – one that will return a valid HTML page to the browser if I typed just the website address (http://mydomain.com/mywebsite/ )
even if I do not enter a page name. Of course in most cases on your web server that is set to be index.php, index.html or default.php etc. I mostly use index.php.

So I created as simple index.php – made sure it shows up on a browser and the tried to put the website address (without the page name, as initially instructed by DW) and everything worked.
So when you are at that stage of setting up a new site on DW, in addition to setting the website (which you probably did perfectly already) go ahead and add a default page (if PHP server technology a index.php) and test it responds to the website address. This should let the DW go through that stage perfectly.

Obviously Dreamweaver has no way to identify a healthy response from the website unless there is a page that returns valid HTML when the website address (without a pages name) is requested.

I guess if you are using a different technology – .NET or Cold Fusion you will need a default page like that in the respective technology or simply in html.

Happy webmastering!

TopWebGuy.com

TopWebGuy.com Offer Webmaster Services, Web Desing, eCommerce Solutions

Monday, April 12th, 2010

TopWebGuy.com Offer webmaster services, web desing, eCommerce solutions, back-end integration, complete IT consulting.