October 21st, 2009
One of our friends just launched a new amazing portal with free online games. You’ll find everything from action games and racing games to good old classic retro games or something for your kids. So, if you need a break check out Ruula.com.
Have fun and enjoy!
Posted in Uncategorized | No Comments »
October 19th, 2009
I have installed Postgresql database on my server:
sudo apt-get install postgresql
and after installation my first question was: Ok, what’s the password?
The answer is simple: you can always change the password and set new one. Just do it that way:
alex@server:/home/alex# su postgres
postgres@server:/home/alex$ psql -d template1 -U postgres (connect to database)
template1=# alter user postgres with password 'new_password'; (change password to 'new_password')
\q (exit)
Done
Tags: change, password, postgresql
Posted in postgresql | No Comments »
September 15th, 2009
LOAD DATA INFILE is very useful function if you need to import to database txt or csv file. Just call it like this:
LOAD DATA INFILE /var/www/mypage/source/init.txt INTO TABLE init_data FIELDS TERMINATED BY '|';
and all data will be inserted into your table. It is much faster way than create loops, read file line by line, prepare inserts and insert every row into database. Really nice solution, but… you have uploaded the file, run query and see the error:
failed to execute the SQL statement: SQLSTATE[HY000]: General error: 29 File ‘/var/www/mypage/source/init.txt’ not found (Errcode: 13)
and you are 100% sure that this file exists.
Okay, maybe it is chmod problem? Changed it to 0777 and still nothing. Maybe file is not readable? Just wrote code with fopen, fgets, echo, fclose. Strange, file is opened and data are sent to output. But MySQL still does not see the source. So, maybe problem is with this source?
Not exactly, the problem is with path…. On Ubuntu Linux Apparmor does not allow mysqld getting files from /var/www/ folder. Just move your file to /tmp and voila, data are inserted into database.
I don’t know how to change it in Ubuntu, but I have moved the file and it solved my problem. If you know the solution, please share it with us.
Enjoy
Alex
Tags: file not found, import, load data infile, mysql, PHP
Posted in Linux / Ubuntu, PHP, Uncategorized, mysql | No Comments »
September 15th, 2009
When you are working on MySQL database, usually you use UTF8 encoding. It’s most popular and flexible to store all texts on webpage. It works really well, but… sometimes you need to import some data. For example – from external file. Customer delivers you text file, you quickly wrote script with some inserts and you see strange error:
failed to execute the SQL statement: SQLSTATE[HY000]: General error: 1366 Incorrect string value: ‘\xE9e des…’ for column ‘Address1′ at row 5′
or something similar.
Really strange, and where’s the problem?
So, it looks that \xE9e or another string starting from \x causes it. \xE9e is nothing more but UTF8 representation of letter é. Problem is that it is not stored in two bytes, but as special escaped string and MySQL could not understand it.
I spent all evening trying to find solution and when discovering php.net I found small, but very useful function (source):
function fixEncoding($input)
{
$cur_encoding = mb_detect_encoding($input) ;
if($cur_encoding == "UTF-8" && mb_check_encoding($input, "UTF-8"))
return $input;
else
return utf8_encode($input);
}
Just use this method on your string you want to insert and it will be encoded properly.
Enjoy
Alex
Tags: encoding, error, incorrect string, mysql, PHP, utf8
Posted in PHP, Uncategorized, mysql | No Comments »
September 15th, 2009
You have started new project. Worked hard every night, and voila, the best code in the world is finished. You tested it many times, and show your work to the customer, so he is happy. You love that work!
You deliver all sources and database dumps to the customer and start next project.
Next day you have your customer on your phone with one big word: WTF??? Looks that code does not work, but why? When you are running scripts on customer’s server, you get MySQL messages with different messages:
SQLSTATE[HY000]: General error: 1364 Field ‘RecordCreated’ doesn’t have a default value
or
ERROR 1292 (22007): Incorrect datetime value: ‘0000-00-00 00:00:00′ for column ‘DateModified’ at row 1
or someting similar.
You really don’t know what’s going on – you are 100% sure that your code works on your local environment.
So… Maybe problem is with your environment?
If you install wamp server or another environment on local machine, MySQL works in ANSI mode. It is the mode, closest to SQL standard, allowing many warnings to be ignored when working on data. But much more strict mode is TRADITIONAL – described as: give an error instead of a warning” when inserting an incorrect value into a column. It always return error and breaks executing the code when some data are incorrect. It is great mode for developing – you will be sure that if your code works in TRADITIONAL mode, it will work in ANSI mode. If you want to read more, just look here.
Enjoy
Alex
Tags: ansi, general error, incorrect value, mode, mysql, traditional
Posted in PHP, Uncategorized, mysql | No Comments »
July 1st, 2009
If you need to insert rows into your table, but don’t want to check, is that value exists or not in table, there is very useful feature of MySQL - INSERT ... ON DUPLICATE KEY UPDATE syntax.
- Normally if you want to check row, you have to run SELECT query and if row does not exist, run INSERT query (and you need to use 2 queries).
- You may use REPLACE INTO… syntax, but it firstly tries to delete old row and then inserts new one, so you can lose your autoincremented ID (updated row has another ID) and it can cause a lot of problems with dependencies.
The solution is:
INSERT ... ON DUPLICATE KEY UPDATE syntax.
I use it that way:
INSERT INTO tbl1 (ArticleName, ArticleBody) VALUES ('Article1', 'Body1')
ON DUPLICATE KEY UPDATE ArticleName = 'Article1', ArticleBody = 'Body1';
or more useful if you synchronize two tables:
INSERT INTO tbl1(ArticleName, ArticleBody) SELECT ArticleName, ArticleBody FROM tbl2
ON DUPLICATE KEY UPDATE ArticleName = tbl2.Article1, ArticleBody = tbl2.ArticleBody;
Note: One of that fields has to have UNIQUE index.
First run of the query inserts records. Second run inserts new records and updates existing records. If your source table grows, running this query synchronizes tables in very simple way.
Another advantage of this syntax is running counters. For example, if we have table like:
CREATE TABLE views (
IP VARCHAR( 100 ) NOT NULL ,
ViewCounter INT NOT NULL ,
UNIQUE (IP)
) ENGINE = MYISAM
IP is UNIQUE, so we can always count our page’s views:
INSERT INTO views (IP, ViewCounter) VALUES ('192.168.0.1', 1)
ON DUPLICATE KEY UPDATE ViewCounter = ViewCounter + 1;
First run inserts record with value 1. Every next run of this query increases the counter
If you want to count daily views, you can use it that way:
CREATE TABLE Views (
IP VARCHAR( 100 ) NOT NULL ,
ViewDate DATE NOT NULL ,
ViewCounter INT NOT NULL ,
UNIQUE (IP, ViewDate)
) ENGINE = MYISAM
IP and ViewDate are UNIQUE pair, so we count our page’s views:
INSERT INTO views (IP, ViewDate, ViewCounter) VALUES ('192.168.0.1', '2009-07-01', 1)
ON DUPLICATE KEY UPDATE ViewCounter = ViewCounter + 1;
Enjoy
Alex
Tags: count, insert, into, mysql, replace
Posted in Uncategorized, mysql | No Comments »
June 20th, 2009
The most used collation for databases is UTF8, but there are some variations – for example utf8_unicode_ci, utf8_general_ci etc. If you created your database, you are always sure that used collations are the same. But sometimes if you integrate your database with someone’s database, you may experience differences in collations. So if you call query like:
SELECT * FROM tbl1 JOIN tbl2 ON tbl1.Name = tbl2.Name
you can see the error message like:
Illegal mix of collations (utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation '='
The problem is with different collations. So the solution is to use CONVERT function:
SELECT * FROM tbl1 JOIN tbl2 ON CONVERT(tbl1.Name USING utf8) = CONVERT(tbl2.Name USING utf8)
Enjoy
Alex
Tags: collation, convert, error, mysql, utf8
Posted in mysql | No Comments »
May 3rd, 2009
Introduction:
Network-shared printers on Vista work terribly slow. Every time when I was trying to print any document I had to wait up to few minutes just for the response (depending on document size and complexity). Even changing the printer properties on Vista was taking forever.
What’s more, opening documents in MS Word with network-shared printer set as default was also sluggish (because Word is connecting printer and adjusting formatting to match printers margins etc.)
Funny thing was the fact, that the printing on the same printer (shared on W2k8 or other Windows) from Windows XP was as quick as a flash.
After some investigation I’ve found that Vista is not really installing printer in your system but simply adding camouflaged remote printer. In practice it means that you can use this printer as your own but that technique utilizes tons of communication between server and your system every time you’re trying to print sth. That means slow…
Problems:
- extremely slow printing on network-shared printers
- slow opening documents in MS Word and other editors
- delayed access to printer settings and properties
Solution:
After spending some time with google I came across this support site from Microsoft: http://support.microsoft.com/kb/280821
The simple trick:
Just add a new printer as a local printer (use proper drivers), then create a new local port in printer properties, naming it with the printer’s network path – like “\\server\my_printer”.
Works perfectly.
Summary:
- using proper drivers add new local printer
- in the printer properties, on the “Ports” tab add a new port
- choose “Local port”
- name it with the network path to the printer (like “\\server\my_printer”)
- now you can go celebrate
From the link above you can get more details.
Tags: network printer, network printing, printer, printing, shared printer, slow, vista
Posted in Uncategorized | No Comments »
April 17th, 2009
To generate object from dynamic class name You can use flash.utils.getDefinitionByName function.
Example:
var dynamicClassName:String = “namespace.MyClassName”;
var ClassReference:Class = getDefinitionByName(dynamicClassName) as Class;
var element:Class = new ClassReference();
ReferenceError: Error #1065
However You have to remember to just make object declaration of class MyClassName, cause importing this class won’t make a difference for the compiler – if the class is not used in the code at least once it’s not included in output swf when compiling. Another way to do it in Flex is to include compiled swc library with dynamic classes into compiler directive:
-include-libraries “myDynamicClasses.swc”
You have to see which solution would have better performance for Your application.
Tags: AS3, class name, class reference, Flex, getDefinitionByName, object, ReferenceError: Error #1065, unknown class
Posted in Flash, Flex 2/3 and AS3 | No Comments »
March 31st, 2009
Unfortunately there is no possibility to monitor upload when using UrlLoader. First event (Event.OPEN) starts when first response comes back from server, then ProgressEvent starts to monitor transfer. It’s made only for download purposes, no matter if You use POST or GET. However If You’re not sending dynamic data generated in Flash, but existing file You can still use FileReference (in Flash, Flex and AS3) or dynamic data by HTTPService (in Flex).
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/FileReference.html
http://livedocs.adobe.com/flex/3/langref/mx/rpc/http/HTTPService.html
Tags: AS3, Flash, Flex, GET, issue, POST, problem, ProgressEvent, UrlLoader
Posted in Flash, Flex 2/3 and AS3 | No Comments »