Insert arabic text by mysql command prompt or terminal

Suppose you are trying to import sql file via command prompt having arabic fields values in it . In this case , Although you have alter collation of field to utf8_general_ci . But it would insert arabic text in readable form in database instead of converting that text into Binary and then utf8 which is wrong and you might face issue in showing arabic text in php.

It is possible that converting MySQL dataset from one encoding to another can result in garbled data, for example when converting from Latin1 to UTF8. There are a number of approaches. All examples assume we are converting the title VARCHAR (255) column in the comments table.

It is possible that converting MySQL dataset from one encoding to another can result in garbled data, for example when converting from Latin1 to UTF8. There are a number of approaches. All examples assume we are converting the title VARCHAR(255) column in the comments table.

#1 Convert to blob then UTF8

The first approach is via Mattias and Perconas:

ALTER TABLE comments MODIFY title BLOB;
ALTER TABLE comments MODIFY title VARCHAR(255) CHARACTER SET utf8;

The same Percona link also has more approaches on how to do this without locking the tables.

#2 convert to BINARY then UTF8

Sidecar has a nice simple 1-liner both to test the results and convert it and a little Ruby script to automate over the entire DB. The test snippet is particularly useful. This doesn’t change the column (no ALTER TABLE), instead it just converts the data in place.

-- test
SELECT CONVERT(CAST(CONVERT(title USING latin1) AS BINARY) USING utf8) FROM comments WHERE id = 123;

-- convert
UPDATE comments SET title = CONVERT(cast(CONVERT(title USING latin1) AS BINARY) USING utf8);
ALTER TABLE comments MODIFY title VARCHAR(255) CHARACTER SET utf8;

#3: iconv

Another other approach is to use the iconv command to convert the an entire table:

mysqldump comments --add-drop-table users | replace CHARSET=latin1 CHARSET=utf8 | iconv -f latin1 -t utf8 | mysql some_database

Additionally Percona offers a tool to convert the character-set for an entire DB as discussed in their converting characterset post.

#4: Safest approach without double conversion (PREFERRED METHOD)

It is possible that you are still ending up with garbled data or something is going wrong. This might happen for example if you have stored UTF8 data in a LATIN1 column or some other such scenario. Joni Salonen offers a check along with the update.

UPDATE comments SET title = @txt WHERE char_length(title) =  LENGTH(@txt := CONVERT(BINARY CONVERT(title USING latin1) USING utf8));
ALTER TABLE comments MODIFY title VARCHAR(255) CHARACTER SET utf8;

Debugging Tip

If you feel like digging deeper, you can use the HEX function to test the output, example:

 

SELECT HEX(CONVERT(`title` USING latin1)) FROM `comments` WHERE id=123;
SELECT HEX(CONVERT(CAST(CONVERT(title USING latin1) AS BINARY) USING utf8)) FROM comments WHERE id = 123;
Advertisements

subl command work for your mac

create the symlink in /usr/local/bin instead of ~/bin and make sure that /usr/local/binin in PATH.

$ ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/.
$ echo $PATH

If you don’t find /usr/local/bin/, then add the following lines to your .bashrc or .zshrc

PATH=$PATH:/usr/local/bin/; export PATH

Performs a smooth page scroll to an anchor on the same page.

$(function() {
$(‘a[href*=#]:not([href=#])’).click(function() {
if (location.pathname.replace(/^\//,”) == this.pathname.replace(/^\//,”) && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $(‘[name=’ + this.hash.slice(1) +’]’);
if (target.length) {
$(‘html,body’).animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});

});

REST Vs SOAP, The Difference Between Soap And Rest

Someone asked me a question today “Why would anyone choose SOAP (Simple Object Access Protocol) instead of REST (Representational State Transfer)?” My response: “The general rule of thumb I’ve always heard is ‘Unless you have a definitive reason to use SOAP use REST’”. He asked “what’s one reason?” I thought about it for a minute and honestly answered that I haven’t ever come across a reason. My background is building great internet companies.

While he seemed satisfied, I wasn’t very happy with that answer, I did some homework and here’s my summary on REST versus SOAP, the difference between SOAP and REST and why anyone would choose SOAP. As usual, with competing technologies both have value, the challenge is to know when to use each one (spoiler: luckily the answer is almost always REST).

I’m clearly boiling down a somewhat so please don’t flame me for simplifying things, but feel free to provide any corrections you feel are necessary.

Definitions

REST

RESTs sweet spot is when you are exposing a public API over the internet to handle CRUD operations on data. REST is focused on accessing named resources through a single consistent interface.

SOAP

SOAP brings it’s own protocol and focuses on exposing pieces of application logic (not data) as services. SOAP exposes operations. SOAP is focused on accessing named operations, each implement some business logic through different interfaces.

Though SOAP is commonly referred to as “web services” this is a misnomer. SOAP has very little if anything to do with the Web. REST provides true “Web services” based on URIs and HTTP.

By way of illustration here are few calls and their appropriate home with commentary.

getUser(User);

This is a rest operation as you are accessing a resource (data).

switchCategory(User, OldCategory, NewCategory)

This is a SOAP operation as you are performing an operation.

Yes, either could be done in either SOAP or REST. The purpose is to illustrate the conceptual difference.

Why REST?

Here are a few reasons why REST is almost always the right answer.

Since REST uses standard HTTP it is much simpler in just about ever way. Creating clients, developing APIs, the documentation is much easier to understand and there aren’t very many things that REST doesn’t do easier/better than SOAP.

REST permits many different data formats where as SOAP only permits XML. While this may seem like it adds complexity to REST because you need to handle multiple formats, in my experience it has actually been quite beneficial. JSON usually is a better fit for data and parses much faster. REST allows better support for browser clients due to it’s support for JSON.

REST has better performance and scalability. REST reads can be cached, SOAP based reads cannot be cached.

It’s a bad argument (by authority), but it’s worth mentioning that Yahoo uses REST for all their services including Flickr and del.ici.ous. Amazon and Ebay provide both though Amazon’s internal usage is nearly all REST source. Google used to provide only SOAP for all their services, but in 2006 they deprecated in favor of REST source. It’s interesting how there has been an internal battle between rest vs soap at amazon. For the most part REST dominates their architecture for web services.

Why SOAP?

Here are a few reasons you may want to use SOAP.

WS-Security

While SOAP supports SSL (just like REST) it also supports WS-Security which adds some enterprise security features. Supports identity through intermediaries, not just point to point (SSL). It also provides a standard implementation of data integrity and data privacy. Calling it “Enterprise” isn’t to say it’s more secure, it simply supports some security tools that typical internet services have no need for, in fact they are really only needed in a few “enterprise” scenarios.

WS-AtomicTransaction

Need ACID Transactions over a service, you’re going to need SOAP. While REST supports transactions, it isn’t as comprehensive and isn’t ACID compliant. Fortunately ACID transactions almost never make sense over the internet. REST is limited by HTTP itself which can’t provide two-phase commit across distributed transactional resources, but SOAP can. Internet apps generally don’t need this level of transactional reliability, enterprise apps sometimes do.

WS-ReliableMessaging

Rest doesn’t have a standard messaging system and expects clients to deal with communication failures by retrying. SOAP has successful/retry logic built in and provides end-to-end reliability even through SOAP intermediaries.

Summary

In Summary, SOAP is clearly useful, and important. For instance, if I was writing an iPhone application to interface with my bank I would definitely need to use SOAP. All three features above are required for banking transactions. For example, if I was transferring money from one account to the other, I would need to be certain that it completed. Retrying it could be catastrophic if it succeed the first time, but the response failed.

cakephp mapping $belongsTo association to a non primary key

I was told that to accomplish mapping the $belongsTo to a non primary key, I would set the foreignKey to false and set the conditions

var $belongsTo = array(

    'Inventory' => array(
        'className'    => 'Inventory',
        'foreignKey' => false,
        'conditions' => array('RentalLineitem.i_num = Inventory.i_num'),
        'dependent'    => false
    )

);

session_start ( PHP 4 , PHP 5 )

session_startStart new or resume existing session

Description

bool session_start ( void )

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. These will either be a built-in save handler provided by default or by PHP extensions (such as SQLite or Memcached); or can be custom handler as defined by session_set_save_handler(). The read callback will retrieve any existing session data (stored in a special serialized format) and will be unserialized and used to automatically populate the $_SESSION superglobal when the read callback returns the saved session data back to PHP session handling.

To use a named session, call session_name() before calling session_start().

When session.use_trans_sid is enabled, the session_start() function will register an internal output handler for URL rewriting.

If a user uses ob_gzhandler or similar with ob_start(), the function order is important for proper output. For example, ob_gzhandlermust be registered before starting the session.

Return Values

This function returns TRUE if a session was successfully started, otherwise FALSE.

Changelog

Version Description
5.3.0 If a session fails to start, then FALSE is returned. Previously TRUE was returned.
4.3.3 As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

Examples

Example #1 A session example: page1.php

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']   = 'cat';
$_SESSION['time']     = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

After viewing page1.php, the second page page2.php will magically contain the session data. Read the session reference for information on propagating session ids as it, for example, explains what the constant SID is all about.

Example #2 A session example: page2.php

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];   // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

Notes

Note:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Note:

Use of zlib.output_compression is recommended instead of ob_gzhandler()

Note:

This function sends out several HTTP headers depending on the configuration. See session_cache_limiter() to customize these headers.

Custom Configure files in CakePHP

A little while ago I wrote about using environment variables to store various sensitive information in your application without revealing it in source control.

As always, there is more that one way to do anything, so today I wanted to write about CakePHP’s built-in functions that can be used to manage application specific settings.

Most (all?) CakePHP developers should be familiar with the app/config/core.php file, which contains the core configuration for the application. If nothing else, you would be familiar with changing the salt anddebug settings in this file as part of your CakePHP install/deployment

<?php
Configure::write('debug', 2);
Configure::write('Security.salt', 'sdasdwrwsert456456456tfdfdgdfgdfgxcx');

However, you can also create your own versions of a configuration file with all your app specific settings.

Create a custom config

First off, create a file in the app/config directory named however you want.

Example: my_app_settings.php

Now in this file you can add any settings you want using the $config array

<?php
$config['MyApp']['mysql_user'] = 'bob';
$config['MyApp']['mysql_pass'] = 'foobar';
// etc etc

Next up, you need to tell your app to load this new config file. I tend to do it in the bootstrap.php to ensure it is loaded throughout the app (although if there is a better/more advisable place be sure to let me know)

<?php
// app/config/bootstrap.php

// snip

Configure::load('my_app_settings');

Now anywhere in your app you have access to your custom settings

<?php
$mysqlUser = Configure::read('MyApp.mysql_user');

Protect your credentials

The last thing you want is your third party API credentials, or database logins being made available to anyone via source control. In general, best practice states you shouldn’t store any security information in your source control.

Well this provides a perfect solution. Create a copy of the my_app_settings.php calledmy_app_settings.example.php

Ensure that all settings your app needs are included, but make sure the values are zeroed out and commented to ensure others know what is required.

<?php
$config['MyApp']['mysql_user'] = ''; // this is the user for the mysql database
$config['MyApp']['mysql_pass'] = ''; // this is the password for the mysql database
// etc etc

Then add the example to your source control so other developers know how to configure the application, but make sure your live version is added to the source control ignore list. This simple setup should prevent any sensitive information accidentally leaking out.

Deployment

Having done the above, you now find that deploying leads to a broken application as there are no app settings available.

This can be fixed by storing a copy of the settings file specific to the environment the deploy targets which is copied into the app/config path during the deploy

If you are using Capistrano for example, this can be done by storing a copy of the settings file in the shared folder, then symlinking or outright copying it over to the current version during the deploy.