Last night I ran into the crazy problem that a mysql result set contained rows (confirmed with mysql_num_rows) but I wasn’t able to fetch the rows in mysql_fetch_array while loop construction. I managed to create a workaround but I posted a note on the php.net mysql_fetch_array page asking for an explanation. The note got rejected so I figured I could use my own blog in the search for an answer.

Here is the note;

I hope someone can help me with this question.

I have 2 code snippets. The first works, the second doesn’t. I would prefer using the second code snippet because it seems cleaner. What’s wrong with the second code snippet?

This works;

$results = mysql_query("SELECT DISTINCT(col) FROM table WHERE col!='' ORDER BY RAND()");
for( $i = 0; $i < mysql_num_rows( $results ); $i++ )
{
// do something with mysql_result($results,$i,0);
}

This doesn’t work;

$results = mysql_query("SELECT DISTINCT(col) FROM table WHERE col!='' ORDER BY RAND()");

 while($result = mysql_fetch_array($results))
 {
 //do something with $result
 }

I’m writing a web application that allows it’s users (among other things) to upload their company logos. PHP script is used to take the user provided jpgs/pngs and present them uniformly in a webpage. I ran into the problem that rescaled png images had their transparent regions turned black.

Here’s the solution I found;

Voila.

Although the last step effectively overwrites all the pixels in your image, step 2 and 3 seem to be absolutely necessary to allow transparency inside your png image.

One more word of advice; use imagecopyresampled instead of imagecopyresized.

Resampled

Resampled "Dutch Design Award"

Resampled "Dutch Design Award"

Resized

Resized "Dutch Design Award"

Resized "Dutch Design Award"

Okay earlier on in ‘findability-and-linkability-with-flash‘ I pointed to the usefull php function strip_tags that is an easy alternative to writing your own regular expressions to extract certain HTML tags from a string.

Strip_tags proved useful for removing ActionScript from a text for the purpose of reuse of that text in an HTML context. This is because the ActionScript was embedded in anchor-tags for example like:

<a href=”asfunction:openURL,http://www.studioroosegaarde.net“>Studio Roosegaarde</a>

The price you pay for the simplicity of using strip_tags is that with the removing of the ActionScript some functionality goes lost to. The ActionScript function openURL was in the Flash context responsible for opening the requested page in a new window. So where “Studio Roosegaarde” used to be a hyperlink it now does nothing.

If only we could extract the hyperlink-part from the original text it would be so easy to construct the same functionality in a HTML context. The good news is with regular expressions we can. The php implementation goes like this:

//replace openURL actionscript with html hyperlinks
$txt = eregi_replace(“<[^>]*asfunction:openURL,([^\"']*)\”[^>]*>([^<]*)</a>”,
“<a href=\”\\1\” target=\”_blank\”>\\2</a>”,
$txt);

Really this looks far harder then it is. It took me 10 minutes to figure this out. I used the wikipedia article on regular expression for getting a general understanding of regular expressions and then the documentation of eregi_replace – a php function that uses regular expression as pattern and replace string for replacing ignoring case. The only shortcoming with the regular expression that I produced is that it does not work if html tags are nested inside the anchor element. For example like this:

<a href=”"><b>Linkje</b></a>

If you know how to solve this please tell me!

If you, like me you never found the energy to investigate how regular expressions work. Just do it right now. It’s not that hard and the results are really rewarding. Knowing how to use regular expressions is in the long run far more powerful then finding some language specific solutions to your problem – like using strip_tags in php – as regular expressions can be used in all programming environments.

I encountered a problem with displaying a “loader” (a progress-bar) for downloading a dynamically generated XML. The loader would remain 0% until the XML was completely loaded. The problem is that the web server providing the XML didn’t send file-size information in it’s HTTP headers, as it doesn’t know file-size prior to generating the XML. Here is a trick that you can use when using PHP code on the server-side to wait for the XML generation process to finish before serving the client, so that the file-size can be put in the HTTP headers.

ob_start()

ob_start(‘ob_gzhandler’);

… output the XML content…

ob_end_flush();

header(‘Content-Length: ‘.ob_get_length());

header(“Content-Type: text/xml”);

ob_end_flush();

Follow

Get every new post delivered to your Inbox.