Warning: Invalid argument supplied for foreach()

A common error which occurs with foreach loops is the standard “Invalid argument supplied for foreach()” whch gets thrown up as a warning.

This can easily be overcome by wrapping the foreach loop in a conditional if statement which checks to see if the argument supplied to the loop is an array or not.

Below is an example of how to correct the problem:

if (in_array($arrItem)) {
foreach($arrItem as $arrItemi) {
// use $arrItemi;
}
}

Unable to connect to any of the specified MySQL hosts.

If you get the following error while trying to connect to a MySQL database using a third party client such as TOAD or SQL Workbench, then you need to edit your mysql server’s my.cnf file.

Error Message: Unable to connect to any of the specified MySQL hosts.

So open the my.cnf file by running a similar command to the following one dependent on your server’s setup:

vi /etc/mysql/my.cnf

Then edit the bind-address from what is probably set as 127.0.0.1 (also known as localhost) to your server’s remotely accessible IP address.

If you are unsure what your server’s IP address is then you need to ping it from the command line on another machine outside it’s network (e.g. your own machine) as follows:

ping yourhost.com

N.B. If you are running a Dovecot/Postfix type email gateway setup on the same machine, changing your mysql bind-address will affect it’s connection to the database and you will no longer be able to send/receive emails through it. So you will have to adjust your Dovecot/Postfix configuration files where relevant.

PHP nl2br on one line

I usually store data from a textarea directly to the database as is, then once I retrieve it back into HTML I use that lovely PHP function nl2br() to convert it to HTML
tags.

This works well for most cases until you are passing this data back into javascript where everything has to be on one line!

nl2br() outputs as follows:

Line one

Line two

And I want it to be as follows:

Line one
Line two

So how do I do this?

echo trim(strtr($myOriginalString, array("\r\n" => "<br />", "\r" => "<br />", "\n" => "<br />")));

IE9 adding empty table cells in large table

Well as usual, we are once again fighting the woes of the beloved Internet Explorer, and it appears that Microsoft’s latest greatest new and improved browser rival to the market IE9 still has a whole bunch of irritations and retardation to worry about (saw that one coming).

Today I was busy loading a massive amount of data into the body of a table using ajax and in Firefox and even Internet Explorer 8 it worked quite well, but Internet Explorer 9 decided that it would like to add some empty table cells randomly around my “massive table”, now of course I began by kicking something and cried a few times….. but then I thought about whitespacing and the usual problems I had found with it before while using “the best browser ever known to human-kind”(…not) and I thought of removing the whitespaces between the <tr>’s and <td>’s.

Sample code while crying and trying to get rid of the ironious cells:

<table>
<tbody>
<tr>
<td>Some text here 1</td>
<td>Some text here 2</td>
<td>Some text here 3</td>
<td>Some text here 4</td>
</tr>
</tbody>
</table>

The really dumb thing that fixed IE9′s problems:

<table>
<tbody>
<tr><td>Some text here 1</td><td>Some text here 2</td><td>Some text here 3</td><td>Some text here 4</td></tr>
</tbody>
</table>

 

 

And voila! Remove the whitespaces yourself and IE9 won’t get confused!

MySQL Error: Can’t get hostname for your address

This error is thrown by MySQL because it tries to do a DNS lookup on the address connecting to the server and it fails due to the IP not being able to be resolved back to a name or some-other deeper DNS bust problem.

The easiest way to fix this is to edit your my.ini or my.cnf (/etc/my.cnf) and in the
[mysqld] section add the “skip-name-resolve” option.

Near the top of the file this will look similar to the below:

[mysqld] skip-name-resolve

Adobe drops flash for mobiles!

..and it’s official – so says Adobe’s blog (read it here). Adobe will be dropping support for Flash on future devices due to performance and a few other things, pushing the future more towards the HTML5 route, which isn’t neccessarily a bad thing, I just find it quite ironic and somewhat comical that they waited for Steve Jobs (Apple) to die before giving up their long time fight with Apple about Flash being or not being supported on mobile devices, namely the iPhone.

Read more here: http://blogs.adobe.com/flashplatform/2011/11/flash-to-focus-on-pc-browsing-and-mobile-apps-adobe-to-more-aggressively-contribute-to-html5.html