Unicode error using PDO. Mysql Unicode problem

Are you having those ???? things on your webpage when something you entered on Mr mysql is unicode character?
Here is a fix for that

Assuming you are using PDO object in your Db Class..

$db = array containing information ....
$this->pdo = new PDO("mysql:host={$db['host']};dbname={$db['database']};charset=utf8", $db['username'], $db['password']);

Now the next task to to tell Mysql to honor unicodes:

$this->query('SET NAMES utf8');
$this->query('SET CHARACTER SET utf8');
$this->query('SET COLLATION_CONNECTION="utf8_general_ci"');

where your query function could look like

public function query($query, $params=array())
	{
		$results=array();
		if (!empty($this->pdo))
		{
			try
			{
				$stmt=$this->pdo->prepare($query);
				$stmt->execute($params);
				$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
			}
			catch(PDOException $exception)
			{
				echo "error occured";
				//logit or whatever
				die();
			}
		}
		return $results;
	}

Yea this would take care of the problem. There are certain things you have to do on the mysql database itself to handle unicode but I will not discuss those here
Hope it would help somebody..

Listing all the curl constants and viewing them in PHP – how to see the curl opt integer constants

Here is the thing :), I was working on some API where there is an example of cURL usage for it. For some weird reason the developer has decided to use the php cur option integer constant rather than the nice constant names [listed here].
So i have to use this little snippet to reverse match them..
No biggie, just sharing

$all_constants=get_defined_constants(true); // list all defined constants in the world
$curl_opts=array_flip($all_constants['curl]);
ksort($curl_opts);
print_r($curl_opts);

Firefox adds xls extension when downloading excel files from problem

My problem is this.
I have this excel report file that I crafted using PHPExcel for my php application. I don’t have any problem accessing the file from my ubuntu machine with major browsers. But from my windows box, FF will add this .xls extension on top of my file name [myreport.xlsx becomes myreport.xlsx.xls] and this would confuse windows on how to create and will finally decide to open it as binary gibberish texts.
The solution is to assign the correct MIME type for the xlsx extension. I got that gem from here

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

phtml, php5 php3.. do not render on Apache webserver solution

Php has more extension than the customary ‘.php’.
If you are using for some reason the extension other than php, then you have to let Mr Apache that you intend to treat .phtml just like that of php files.
You can accomplish this using a simple .htaccess file that sits on the root folder of your website.

AddHandler application/x-httpd-php .php .php3 .php4 .php5 .phtml .html .htm

Yup that should take care of the mess.

Installing Imagick for php using pecl

If you are dealing with CAPTCHA or some graphics stuff you have definitely come across with Imagick.. it is a nice library that you should check out..
to install.. it is much really easy with pecl
here is the step by step process to install it..

pecl download imagick
tar -zxvf imagick-VERSION.NUMBER.GOES.HERE.tgz
cd imagick-VERSION.NUMBER.GOES.HERE
phpize
./configure --with-imagick=/opt/local
make
make install

Then, on successful make, you will see the path where the “dot so” is saved..
go to you php.ini file and add

extension=imagick.so

then restart you apache and check if the imagic is available on phpinfo()..

DONe!

handling array inputs in zend. Passing inputs that are generated from javascript to zend controller

Hello There,
Was working on a project that is on zend framework. The task involves having having javascript generated input boxes and passing those to the controller.
I have done that before using some other mechanism but they were not somehow natural ways to do it.
It can be done in a much easier way -NATURALLY- though :)
lets assume your form has javascript powered email address adding inputboxes. There would be some link you would hit as add emails and it will create input boxes for you..

1. have the input box in your zend form as

		$this->addElement('text', 'emails', array(
			'label'        => 'Emails',
			'isArray'      => TRUE,
			'name'         => 'emails[]'
		));

Or however you are creating the inputbox – or anyother input
2. Then in the javascript where you are creating the input, use the same name as the element’s name

	    var new_email = document.createElement('input');
	    new_email.name = 'emails[]';
	    new_email.type = 'text';
	    new_email.setAttribute('size', 30);

3. yes, you are done!. When the form is submitted, the element with name emails[] would be passed along with the other form elements.
In your controller you would have an array of emails[] and the rest is …

Happy zenjsing

Array Merging in PHP preserving keys. array_merge reindexing arrays

Merging arrays in php keeping the keys

Say you have two arrays with the following values in them:


$array1 = array(13=>'bad luck', 7=>'billion people');
$array2 = array(1=>'number');

And you want the final result to be


$final = (1=>'number', 13=>'billion people', 13=>'bad luck');

But when you merge arrays and using array_merge, u got the indexes being ripped out from the second or first array whose keys are numeric?

The result using array_merge would be

(0 => 'bad luck', 1 => 'billion people', 2 => 'number')

And you don’t want that

Here is a simple way – just use operator overloading of the plus sign

$array1 = array(13=>'bad luck', 7=>'billon population');
$array2 = array(1=>'number');
$merged = $array1 + array2;

Note: This is provided the keys are not overlapping, otherwise, the first array would take ownership of keeping the value.

Do you know how to run single phpunit test

Know who is calling your php script browser or script?

localhost shows 403 forbidden in snow leopard mac

In mac, to set up your apache all you have to do is:
Go to terminal and type

sudo apachectl start

– you can get the terminal by going to Application folder and then to utilities and by clicking terminal.

Then go to your browser and type http://localhost.

Now if you get the error 403 forbidden then:
Open /etc/apache2/extra/httpd-vhosts.conf file and make sure you have proper directory setting for example:


    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/Library/WebServer/Documents"
    ServerName local.com
    ErrorLog "/private/var/log/apache2/local.com-error_log"
    CustomLog "/private/var/log/apache2/local_access_log" common

In the above example, the document root is in Library/WebServer/Documents. Which means if I want to create a website, then the website folder has to be inside that and will be accessd as http://local.com/website

**if you name server as localhost, then you can access it as http://localhost

Finally, check if hosts file has correct ip-servername mapping by looking into the file /etc/hosts
Then reload your apache

sudo apachectl restart

EnJoY!

Invalid command VirtualDocumentRoot perhaps misspelled apache failur error

So you got the above error ha? I have been dealing with that as well. Here is the solution.
The root of the problem is mod_vhost_alias.so file.
This file would be used to enable the the mod_vhost_alias. Once this is enabled you are done.

Step 1.
go to

/usr/lib/apache2/modules

and verify the file mod_vhost_alias.so exists. If not, google that file and put it there

Step 2.
go to

/etc/apache2/mods-enabled 

and create a file named vhost_alias.load

vi vhost_alias.load

and put the following inside it

LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so

Step 3.
Do step 2 inside mods-available
Step 4.
restart apache

sudo /etc/init.d/apache2 restart

Step 5
Show :) face!!
This analogy can be used for errors like

Invalid Command ‘SSLEngine’, perhaps misspelled or defined by a module not included in the server configuration. Action ‘configtest’ failed’
and for
Invalid Command ‘rewriteengine’, perhaps misspelled or defined by a module not included in the server configuration. Action ‘configtest’ failed’

undefined variable error javascript with php

you want to do something simple like passing variable like this

<?php 
 $variable_name = "some value";
var some_var = <?=$variable_name" ?>;

but it didn’t work
Here is the solved version…
Very simple than you thought

<pre>
 $variable_name = "some value";
var some_var = "<?=$variable_name" ?>";
</pre>

yup, just quote it from left and right so that it wont be considered as string not JS variable. If the value in variable is number, there would not be any problem