jquery pngFix fixed

I tried to use the pngFix from this website: http://jquery.andreaseberhard.de/pngFix/. But it didn’t work and i received an javascript error in IE6.

After several minutes of debugging and tries i found out that the error is generated by old syntax in selecting elements. It used

jQuery(this).find("img[@src$=.png]").each(...);

instead of

jQuery(this).find("img[src$=.png]").each(...);

So the fix is: Remove the “@” character form the selector string.

The same thing for the “pack” version except that the replaced string is:

[@m$=.M]

and the replacement string is:

[m$=.M]

.

How to dynamically add attributes and child nodes to an XML element in Flex

This is how I copied all the attributes of an XML node (srcXMLNode) to another XML node (dstXMLNode) in Flex, without knowing the names of those attributes:

for each(var attr:XML in srcXMLNode.attributes())
{
dstXMLNode.@[attr.name()] = attr;
}

And this is the way I added child nodes of an XML element to another:

for each(var elem:XML in srcXMLNode.elements())
{
dstXMLNode[elem.name()] = elem;
}

If you know a better way to do this, feel free to write a comment.

Microsoft Outlook 2003 – Downloading Same Email Twice

Most of the emails I was receiving through Microsoft Outlook 2003 I was getting duplicates. The weird thing was that not every email was duplicated and many times I believed that I had fixed the problem (doing various changes in my outlook settings) but I discovered later when automatic checking occurred that I was wrong.

The problem: I discovered that I have each rule duplicated and that’s why Outlook 2003 was downloading the same email twice.

The fix: I removed the duplicated rules.

mysqldump – with exec() function from php outputs empty file

This error occurs on any operating system (windows, linux). The problem is that instead of getting a sql file with the database data you get a empty (0 kb.) file.

So we have the following code:

$command = "mysqldump --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." > backup.sql";

exec($command, $ret_arr, $ret_code);
echo "ret_arr: <br />";
print_r($ret_arr);

and we get an empty file and no output.

So we will fix this error in a few steps:

1. First we need to make sure that we have access to mysqldump command. For Linux machines this command is accessible from anywhere if not you will have to find the place where mysqldump file is (usually the bin folder of mysql).

In order to do this we have to get some output from our command so we will strip all the options from the command and we will remain with this:

$command = "mysqldump"; // mysqldump.exe on Windows

So execute the php script. It’s ok if you get output like this:

Array
(
    [0] => Usage: mysqldump [OPTIONS] database [tables]
    [1] => OR     mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
    [2] => OR     mysqldump [OPTIONS] --all-databases [OPTIONS]
    [3] => For more options, use mysqldump --help
)

If you don’t see something like that then you must check to see the path to the mysqldump command.

If you are in windows make sure you append the full path to the command. If you have a folder like I have C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe with spaces in it you must make sure that you enclose the command between quotes like this:

$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert -h ".$DB_HOST." -u ".$DB_USER." -p ".$DB_PASS." ".$DB_NAME." > backup.sql";

If append the right path to the command and you still cannot get the output then this article can’t help.

2. Make sure you have the rights to create the sql file. This step is mostly for Linux machines where it is very possible that you may try to create a file from php in a folder where you don’t have writing rights.

So to test this after the previous step is done you can do the following: append to the previous command extra options so that the output is not returned but instead written in a file. So we have the previous command:

In Windows:

$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" > backup.sql";

In Linux:

$command = "mysqldump > backup.sql";

After running the file “backup.sql” should  be created.

3.  You must now correct your statement. This means that we must use the long versions of the options like this:

Windows:

$command = "\"C:\\Program Files\\MySQL\\MySQL Server 4.1\\bin\\mysqldump.exe\" --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." > backup.sql";

Linux:

$command = "mysqldump --opt --skip-extended-insert --complete-insert --host=".$DB_HOST." --user=".$DB_USER." --password=".$DB_PASS." ".$DB_NAME." > backup.sql";

The fix: The options for mysqldump when called from php must be in the longer version. Instead of –u use –user, instead of –p use –password and so on.

The attachment of an email sent via PHP has 0 bytes

I wanted to send an email that only contained an attachment as follows:

$headers  = "MIME-Version: 1.0\r\n";
$headers .= "To: <".$to_email.">\r\n";
$headers .= "From: ".$from_name." <".$from_email.">";
$random_hash = md5(date('r', time()));
// add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
// read the atachment file contents from a string previously formed,
// encode it with MIME base64,
// and split it into smaller chunks
$attachment = chunk_split(base64_encode($content));
// construct the body of the message
$message = "--PHP-mixed-".$random_hash;
// my attachment was an html file
$message .= "\r\nContent-Type: text/html; name=\"".$filename."\"";
$message .= "\r\nContent-Transfer-Encoding: base64";
$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";
$message .= "\r\n--PHP-mixed-".$random_hash."--";
// Windows
ini_set('sendmail_from', $return_email);
mail($to_email, addslashes($subject), $message, $headers);
ini_restore('sendmail_from');
// Linux
// mail($to_email, addslashes($subject), $message, $headers, "-r ".$return_email);

I received an email with an attachment, but the problem was that the attached file had 0 bytes.
To my surprise, after comparing the source of the email with the source of an email with a complete attachment, I found out the problem: before the actual content of the attachment ($attachment) there should be an empty line.
I replaced the line of code:

$message .= "\r\nContent-Disposition: attachment\r\n".$attachment."\r\n";

with:

$message .= "\r\nContent-Disposition: attachment\r\n\r\n".$attachment."\r\n"; // notice the double \r\n

and it worked! This way I received the email with the correct attachment.

#1005 – Can’t create table

Full error message: #1005 – Can’t create table ‘.\my_db\#sql-7c4_444.frm’.

Using MySQL version 4.1.22-community-nt, I created two tables (`table1` and `table2`) in a database (let’s call it `my_db`) as follows:

CREATE TABLE `table1` (
`idtable1` int(10) unsigned NOT NULL auto_increment,
`table1_str` varchar(50) NOT NULL default '',
PRIMARY KEY  (`idtable1`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `table2` (
`idtable2` int(10) unsigned NOT NULL auto_increment,
`idtable1_fk` int(10) NOT NULL,
`table2_str` varchar(50) NOT NULL default '',
PRIMARY KEY  (`idtable2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Then I tried to add a foreign key constraint like this:

ALTER TABLE `table2`
ADD CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`idtable1_fk`) REFERENCES `table1` (`idtable1`) ON DELETE CASCADE ON UPDATE CASCADE;

And then I got the error.

The solution in my case was adding the unsigned attribute to the `idtable1_fk` field in `table2` for having the same type as the field `idtable1` it references in `table1`:

ALTER TABLE `table2` CHANGE `idtable1_fk` `idtable1_fk` int(10) unsigned NOT NULL;

After that I ran the foreign key constraint query:

ALTER TABLE `table2`
ADD CONSTRAINT `table2_ibfk_1` FOREIGN KEY (`idtable1_fk`) REFERENCES `table1` (`idtable1`) ON DELETE CASCADE ON UPDATE CASCADE;

And then it worked. But if you don’t have the same problem but you get the same error message you might already have a foreign key constraint with the given name. For example, if I run again the foreign key constraint query I will get the same error. Be careful if you use phpMyAdmin as a visual interface for MySQL, because it seems to me that it does not show us all the foreign key constraints we added to a table. If you want to see which foreign key constraints a table has, you could export the structure of that table and analyze the foreign key constraints queri(es).

Hope this works for you, too.

Conclusion: We should make sure that the possible values of the foreign key field are in the same range as the possible values of the field referenced by that foreign key and that there is no foreign key constraint with the same name as the constraint we are trying to add.

How to uninstall Google Toolbar for Firefox

Problem: Because I did not use Google Toolbar (it was automatically installed when I installed Firefox), I decided to uninstall it. I selected the Uninstall option in Tools -> Add-ons -> Google Toolbar in Firefox (Disable option being grayed) and it said that the toolbar will be uninstalled the next time Firefox will restart. So I closed and started Firefox again and I got a popop window that said something like thanks for installing Google Toolbar and if I want to see the page rank for every site I visit etc. This window kept appearing until I agreed installing Google Toolbar. I also tried uninstalling the toolbar using its uninstall setting, but I kept getting the same (reversed) result.
Everytime I closed the Firefox browser window I got sure that the procees was closed (no instances of FF remained opened).
My operating system is Windows XP Professional.

The solution that worked for me consists of two simple steps:
1. I uninstalled Google Toolbar for Internet Explorer using Control Panel -> Add/Remove programs.
2. I pressed Disable (not grayed anymore) and then Uninstall buttons for Google Toolbar in Firefox Tools -> Add-ons and restarted Firefox.

And it worked! No annoying popop window anymore.

1144: Interface method set data in namespace is implemented with an incompatible signature in class .

Full error message: 1144: Interface method set data in namespace is implemented with an incompatible signature in class.

<package:class><mypackage:myclass></mypackage:myclass></package:class>

This error appears in Flex Builder / Eclipse on the Problems Tab.This error refers to the fact that an extended class named <package.class> with an over ridable function has a correspondent override in <mypackage:myclass> that doesn’t match the overrided function. To be more specific i’ll give you an example:
Let’s say we want to create a custom data grid header renderer and in order to do this we will create a class VerticalHeaderRenderer that will extend DataGridItemRenderer. In the class VerticalHeaderRenderer we will override the setter function for the data property like this:

override public function set data(value:Object):void{
local_var = value.toString();
}

This is the correct version and it will not give the error mentioned in the title but if we have a function that has a parameter of a different type then Object or a different return type we will receive the error.

Basically this error tells us that we are trying to override a function with different parameters or different return value type. So to escape this error you must ensure that the override is made correctly.

Hope this helps.

Good luck.

How to use specific language characters with PHP and MySQL (example: Romanian)

Problem: Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones.

My fix for this problem is using UTF-8 character set encoding for every page of the website and the MySQL tables that contain the fields you are using. Also all the html encodings from PHP use the UTF-8 character set encoding (this is not mandatory).

If you already have the database, but with the default character set (latin1) and collation (latin1_swedish_ci) for the tables with text fields (of type CHAR, VARCHAR, TEXT etc) in which you need to have special characters, you should change the character set of each of those tables like this:

ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;

If you don’t have the database then you should create it and when you create a table that you need to use with specific language characters, you should specify the character set for that table:

CREATE TABLE `my_table` (
`idmy_table` tinyint(3) unsigned NOT NULL auto_increment,
`my_field` varchar(255) NOT NULL default '',
PRIMARY KEY  (`idmy_table`)
) CHARSET=utf8;

The most important thing is that in PHP, after opening a database connection, before executing any query to the database, you should ensure that this code is executed

mysql_query("SET NAMES utf8", $my_conn);

This tells the server what character set the client is using for sending SQL statements and the character set the server should use to return the results to the client.

A simple example:

<?php
$my_conn = @mysql_connect("localhost", "user", "pass")
or die("There was a problem connecting to MySQL. Please try again later.");
if(!@mysql_select_db("test", $my_conn))
{
die ("There was a problem connecting to the database. Please try again later.");
}
mysql_query("SET NAMES utf8", $my_conn);
if(!empty($_GET['mystr']))
{
// insert the string into the database
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";
$result = mysql_query($query, $my_conn);
if($result)
{
// save the id of the table row inserted
$last_insert_id = mysql_insert_id($my_conn);
// get the last inserted value
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";
$result = mysql_query($query, $my_conn);
if($result && $row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$db_string = $row['my_field'] ;
}
}
}
elseif(!empty($_GET['searchstr']))
{
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";
$result = mysql_query($query, $my_conn);
if($result)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$search_results[$row['idmy_table']] = $row['my_field'];
}
}
}
mysql_close($my_conn);
?>
<html>
<head>
<title>Page title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>
<body>
<?php
if(!empty($db_string))
{
echo "<strong>Inserted string</strong>: $db_string<br />";
}
?>
<form method="get" action="">
String to insert into the database <input type="text" name="mystr"/>
<input type="submit" value="GO"/>
</form>
<?php
if(!empty($search_results))
{
echo "<strong>Search results</strong>:<br />";
foreach($search_results as $id => $value)
{
echo $value."<br />";
}
}
?>
<form method="get" action="">
Search query <input type="text" name="searchstr"/>
<input type="submit" value="GO"/>
</form>
</body>
</html>

Tip: The search in Romanian language over the database (tested with MySQL LIKE operator) works like a charm when searching words that have special characters or not.

For example: In Romanian language the word “peasant” is written as “ţăran” and someone who searches it gets the same result for the search terms “taran” or “ţăran” or “ţaran” or “tărân” and so on – so this is the real magic.

UPDATE: You may also need to add a header to the php script if you use ob_start or similar php functions like this:

header("Content-type: text/html; charset=UTF-8");

this usually fixes the encoding selection in Internet Explorer for this case.