Challenges that each developer faces every day: March 2008

Monday, March 31, 2008

A neat way to distinguish development from production environment

The change

Edit local httpd.conf or apache.conf or whatever it is named in your case and put the following variable which will export the MY_ENV_DEVEL the variable with 1 as a value.

SetEnv MY_ENV_DEVEL 1

How to perform the check (PHP) ?

$dev_env = !empty($_SERVER['MY_ENV_DEVEL']) ? 'devel' : 'production';

Some prefer to make this check using a file, but I think this way is much cleaner.

 Digg  Del.icio.us  Reddit  SlashDot

Sunday, March 30, 2008

Zend Framework: set Fetch Mode using setFetchMode

Simple ini style configuration file

[general]
db.adapter = "PDO_MYSQL"
db.config.host = "localhost"
db.config.username = "root"
db.config.password = ""
db.config.dbname = "test"

Loading config
$config = new Zend_Config_Ini('./config/config.ini', 'general');

Setting up database params

$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);

Setting Fetch Mode using setFetchMode

$db->setFetchMode(Zend_Db::FETCH_OBJ);

 Digg  Del.icio.us  Reddit  SlashDot

Zend Framework: getLastInsertId or lastInsertId

Simple ini style configuration file

[general]
db.adapter = "PDO_MYSQL"
db.config.host = "localhost"
db.config.username = "root"
db.config.password = ""
db.config.dbname = "test"

Loading config
$config = new Zend_Config_Ini('./config/config.ini', 'general');

Setting up database params

$db = Zend_Db::factory($config->db->adapter, $config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);

Performing insert
// depending on the columns of your table modify the $data array
$data = array('full_name' => "Example Name", 'active' => 1, );
$db->insert('some_table', $data);

Getting the last inserted ID

$db->lastInsertId()

Related

 Digg  Del.icio.us  Reddit  SlashDot

Friday, March 28, 2008

Mac OS: Can't move .svn/tmp/entries to .svn/entries: Operation not permitted

Did you get the following error on your Mac OS ?

The Problem:
Can't move .svn/tmp/entries to .svn/entries: Operation not permitted

The Solution:

cd working_copy
chflags -R nouchg *

Related:

 Digg  Del.icio.us  Reddit  SlashDot

Wednesday, March 26, 2008

Convert existing MySQL database from one charset encoding to another

Warning: Always make backups.


Convert existing MySQL database from one charset encoding to another

ALTER DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Convert existing table from one charset encoding to another

ALTER TABLE db_table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;

The following commands seem useful (see Related )

Export your latin1 encoded database
mysqldump --user=username --password=password --default-character-set=latin1 --compatible=mysql40 dbname > dump.sql
Import database as utf8
mysql --user=username --password=password --default-character-set=utf8 dbname < dump.sql

Related

 Digg  Del.icio.us  Reddit  SlashDot

svn: Directory .svn containing working copy admin area is missing

The Problem

svn: Directory .svn containing working copy admin area is missing

Explanation

You have accidentally deleted .svn folder.

Solution

Option #1 Checkout the whole project again
svn co http://yoursvn/project/trunk delme

Option #2 Checkout only specific folder from project
svn co http://yoursvn/project/trunk/data/ delme
mv delme/.svn /local/path/project/data/

Note: 'delme' is a folder that you'll want to delete later after your SVN folder is fixed. This is where the files are being checked out

 Digg  Del.icio.us  Reddit  SlashDot

Display MySQL Fulltext Search Settings

MySQL fulltext settings

Fulltext search is only available for MyISAM tables and there is a minimal length of the searched word which is determined by ft_min_word_len variable.

To show current MySQL fulltext settings use the following query in mysql console or in phpMyAdmin SQL box.

SHOW VARIABLES LIKE 'ft_%';

The query should return result like this one:

ft_boolean_syntax + -><()~*:""&|
ft_max_word_len 84
ft_min_word_len 4
ft_query_expansion_limit 20
ft_stopword_file (built-in)

 Digg  Del.icio.us  Reddit  SlashDot

Disabling strict mode in MySQL 5 Windows XP

How to disable strict mode in MySQL 5 Windows XP

Edit /my.ini and look for line

#sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
replace it with

sql_mode='MYSQL40'

Restart the MySQL service (assuming that it is mysql5)

net stop mysql5
net start mysql5
Update: (2008-04-03):

If you have root/admin access you might be able to execute
mysql_query("SET @@global.sql_mode='MYSQL40'");

Related
  • http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
  • http://community.postnuke.com/Wiki-DisablingMySQL5StrictMode.htm
  • http://forum.mambo-foundation.org/showthread.php?p=24856

 Digg  Del.icio.us  Reddit  SlashDot

Sunday, March 9, 2008

Crossplatform Cyrillic-Latin (cyr2lat) Filename Converter

Do you have files that contain cyrilic characters ?

I found an old script of mine (from 2004) that I decided to share with you.

http://funcs.org/downloads/cy2lat/cyr2lat.zip

Note:
For Windows users I've created a GUI (Graphic User Interface) so it will be very easy for you.

If you find any bugs or would like to suggest some improvements feel free to contact me.
(contact info can be found in the ZIP file)

Slavi


 Digg  Del.icio.us  Reddit  SlashDot

Monday, March 3, 2008

How to remove duplicate records with phpMyAdmin?

Sometimes you forgot to add an UNIQUE constraint to a field and you end up having
so many unneeded duplicated records.

My solution is basically the following.

1) Export the table's data
2) Empty the table
3) Set an UNIQUE constraint to a column
4) Reimport data

Here are the details:

This is the most important step, so please pay attention.

1) Exporting the table's data

In phpMyAdmin choose your table
Click -> Export
Click on the following checkboxes (marked in bold are extremely important).

[Structure]
[X] Add IF NOT EXISTS

[Data]
[X] Complete inserts
[X] Extended inserts
[X] Use ignore inserts

I your table is quite big I would suggest you to choose
Compression: (o) "gzipped"

Click the Go button

2) Empty the table
Make sure your export is successful or YOU WILL LOOSE YOUR DATA!!!

Click Empty table and confirm.

3) Set a UNIQUE constraint to a column

Go to Structure tab of your table
Choose the column and click on the U image to make the field UNIQUE

4) Reimport data

Go to Import

Click browse to locate the previously dumped file and then click GO button

Note:
If the file is too big you might have to import the data through ssh.

Upload the YOUR_DATABASE_DUMP.sql.gz

gunzip YOUR_DATABASE_DUMP.sql.gz
mysql --user=YOUR_DB_USER -p YOUR_DB_NAME < YOUR_DATABASE_DUMP.sql
You will be prompted for a password and after several minutes you're done.

Hope it helps.

Disclaimer: Do it on your own risk!


 Digg  Del.icio.us  Reddit  SlashDot