Monthly Archive for May, 2009

Form element name change with JavaScript and radio button

Today I have had to modify very complex script to change look of html form. It is quite easy to make changes in the form. But sometimes it is a problem to modify not to well written PHP script to receive data from new form.
Continue reading…

Sort VARCHAR as INT in MySQL query

If you need to sort varchar data as int, there is one very nice function in MySQL. Problem I had to solve was to sort values as below in a human logical way. Values were similar to this (and I wanted it to be sorted like below):

1; 2; 2a; 3; 4; 5; 5a; 5b; 5c; 6; 7; 11; 14; 14a; 16;

The problem was that if I put this data to MySQL database as varchar and sort by this column, MySQL returned it this way:

1; 11; 14; 14a; 16; 2; 2a; 3; 4; 5; 5a; 5b; 5c; 6; 7;

My second thought was to put is as INTEGER values… It would be sorted as it should but then I would loose values like

2a; 5a; 5b; 5c; 14a;

as it would be converted by MySQL to integer values (2; 5; 14;).

Then I have thought it would be best to put this data to database as a varchar values and sort it as int! After some googling I have found great function that have helped me! The solution was CAST function. To sort it the way I wanted I have just added to my MySQL query this sorting method (pic_number is my field with value to sort):

ORDER BY CAST(`pic_number` AS SIGNED)

and for reverse order

ORDER BY CAST(`pic_number` AS SIGNED) DESC

And it worked like a charm!

Function is as simple to use as:

CAST(expr AS type)

Other possible conversion types you may need are:

  • BINARY[(N)]
  • CHAR[(N)]
  • DATE
  • DATETIME
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • TIME
  • UNSIGNED [INTEGER]

You may read more about this types in MySQL docs here. This function saved me a lot of coding in PHP. I hope it have helped you as much as it helped me!

How to enable error reporting in PHP script

If error reporting is disabled by default on your host you have two solutions.
First is to add to your PHP script this little piece of code:

<?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
  // the rest of your script...
?>

Additionally it is very useful to enable E_STRICT reporting level as well (not included in E_ALL). E_STRICT run-time notices, enable to have PHP suggest changes to your code which will ensure the best interoperability and forward compatibility of your code. To turn it on your code should look like this:

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  // the rest of your script...
?>

But putting this code on the top of your script will not catch any parse errors. A missing “)” or “;” will still lead to a blank page. This is because the entire script is parsed before any of it is executed. If you are unable to change php.ini (that would be the best solution) and set in it

display_errors On

then there is a possible workaround (found on comments on php.net website). This code would solve this problem:

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  include("file_with_errors.php");
?>

You may go even further and make a little debug script with error reporting (when it is disabled for the rest of your scripts). Just create a file called “debug.php” with this content:

<?php
  error_reporting(E_ALL | E_STRICT);
  ini_set("display_errors", 1);
  include($_GET["file"]);
?>

Then you will be able to execute any script on your web host just by calling “debug.php” with urls like this

http://domain.com/folder/debug.php?file=file_with_errors.php

http://domain.com/folder/debug.php?file=subfolder/file_with_errors.php

http://domain.com/folder/debug.php?file=../file_with_errors.php

Second alternative soulution is to edit your .htaccess file. It would enable error reporting for all files in current directory. All you need to do is to simply add the following lines:

php_value display_errors 1
php_value display_startup_errors 1