serving the solutions day and night


Friday, July 30, 2010

Common Code Errors in PHP & MySQL

1. Displaying the current region/country in the top of the list using MySQL statement.
SELECT * FROM country ORDER BY country='US' desc;
An example of social/yellow pages site, want to bring top of the country or state in the list based on the user region. The above code listed US first then rest of other country.

2. MySQL Error - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
  firstname, lastname
  state IN (SELECT state FROM customeryear WHERE companyid = 1 ORDER BY year DESC LIMIT 1);

In MySQL, if SQL statement contains subquery with limt, then you will get the above error. MySQL does not support 'LIMIT & IN/ALL/ANY/SOME subquery' in the newer version, to avoid the error to rewrite the above SQL statement by the below way.

  cust.firstname, cust.lastname, cyear.year
  customer cust INNER JOIN
  (SELECT state, year FROM customeryear WHERE companyid = 1 ORDER BY year DESC LIMIT 1) cyear ON cust.state=cyear.state
  discount>0 and etype ='C';

3.PHP Error - imagettfbbox function

Warning: imagettfbbox() [function.imagettfbbox]: Problem loading glyph in ....

$stextbox = imagettfbbox($sfont_size, 0, $sfontname, $ssecuritycode) or die('Error in imagettfbbox function');

Problem is in the Fonts.
$sfontname = getcwd().'/styles/aerial.ttf';
Font path is not mapped correctly.
Font it not a free version.
Font version, character not included correctly.
Most common in non Latin characters(basically non English characters).
Most common in strings that include characters with acutes or other similar symbols.

4. PHP Error - XsltProcessor class

Fatal error: Class 'XsltProcessor' not found in ....

xsltproc = new XsltProcessor();

Missing xsl module.
On windows, install php_xsl.dll, then uncomment the line from the php.ini file : extension=php_xsl.dll.

On Linux, install module.

5.PHP Error - gregoriantojd() function
Fatal error: Call to undefined function gregoriantojd() ...

$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);

Calender function is missing.
Installing/Configuring, visit

6.PHP Error - unexpected $end in
Parse error: syntax error, unexpected $end in /srv/www/vhosts/ ...

<? ... ?> Use of short open tags in PHP.
Change to full tags <?php ... ?>
Allow short open tags in your PHP.INI file. Look for the SHORT_OPEN_TAG directive in your PHP.INI file and set it to "1".

7.PHP Error - the regex extension is deprecated, calling this function will issue an E_DEPRECATED notice.
Deprecated: Function ereg() is deprecated in
ereg - Regular expression match
Use @ereg() instead of ereg()

8.PHP Error - Warning: strtotime(): It is not safe to rely on the system's timezone settings. ....
Warning: date(): It is not safe to rely on the system's timezone settings. ...

Set the below code, to stop the above warning

9.PHP Error - Deprecated: Function split() is deprecated in
Use explode() function instead of split() function.
explode() function breaks a string into an array.

syntax - explode(separator,string,limit)

$sentence = "Hello Daynight. It's tech blog.";
print_r (explode(" ",$sentence ));

[0] => Hello
[1] => Daynight
[2] => It's
[3] => tech
[4] => blog.

10.Notice: Undefined offset: 3 in cimport.php on line 51
Assign/Get non exist field value. To avoid to get non exist field value, use isset()
$AccountNumber = $data->sheets[0]['cells'][$row][$col];
if isset($data->sheets[0]['cells'][$row][$col])
$AccountNumber = $data->sheets[0]['cells'][$row][$col];

if (isset($_GET['cid'])) $iCompayID = $_GET['cid'];

11.Fatal error: Out of memory (allocated 3057385472) (tried to allocate 32
bytes) in cimport.php on line 27

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in import.php on line 27
1)Open php.ini file, change the values for memory_limit (memory_limit = 8M),
try changing it to 12M or 16M or 24M or -1(unlimited).

2)In your project configuration PHP file, add this line
ini_set(’memory_limit’, ‘12M’);
ini_set('memory_limit', '-1'); (unlimited memory limit)

12.Fatal error: Maximum execution time of 30 seconds exceeded in import.php on line 132
1)Open php.ini file, change the value for max_execution_time (max_execution_time = 30), try changing it to 60 or 120 or 0(unlimited).

2)In your project configuration PHP file, add this line
ini_set('max_execution_time', '60');
ini_set('max_execution_time', '0'); (unlimited execution time limit)

13.Fatal error: Trying to clone an uncloneable object of class XSLTProcessor in
1)Open php.ini file,
zend.ze1_compatibility_mode = On
change On to Off.
zend.ze1_compatibility_mode = Off

2)In your project configuration PHP file, add this line
ini_set ('zend.ze1_compatibility_mode', 0);

14. Trigger Already Exists Error
If you are getting a "Trigger Already Exists" error when you are try to create a trigger, but the trigger is not in information_schema.triggers and does not show when you are issue a "SHOW TRIGGERS" command (which probably looks in the information_schema)

So it means Trigger already exists, but not in information_schema. With the same trigger, so it might be that it left a trace somewhere.

1) Use REPAIR/OPTIMIZE/ALTER on transactional tables corrupt triggers" ( which was fixed in 5.0.20.

2)To be able recreate your trigger you need to remove your_trigger_name.TRN file from MySQL data directory.


Steve Holdoway said...

Example 2. The example can be fulfilled with a basic join, there's no need for a subselect there. Find a proper example.

But, far more important, if a function is deprecated, the correct solution it to re-implement with the new, supported function, and *NOT* to hide the message! A deprecated function is marked to be dropped from the language. That's what the warning's telling you...

Daynight said...

No you can't use LIMIT in the join query or subset query. It is not deprecated problem.
Can you rewrite example 2 using join query?

Bruno Felipe said...

I was monitoring a novice class in DataBase subject and one of the most common mistakes in basic SQL was it. Students used to put "atribute=something" instead of simply put the "atribute" in order to sort the results.

Ivan Jack Ami Pierre said...

Sometime the easier way to make it understandable by student is to show the result of the orded clause in a select, even with a worst arithmetoco-logical expression. It's quite the same as if(a = b) error, but it's less obvious to find it because in reviewing code we often read what we meant not what we wrote.

Such an error is an indicator that the ORDER concept is not clearely stated by the student and five minutes to review that example will avoid definitively the problem for the future.

Clinton Paquin said...

Not sure I would call this a "common" problem, but that is definitely not a strong query!

Richard Lynch said...

#1 is not an error at all.
It does what it was supposed to do.
Put US at the top.

Richard Lynch said...

@ereg() is just as wrong.
Use preg_match instead.

And explode is not always a valid substitute for split.

preg_split is (well, almost always anyway)

Ivan Jack Ami Pierre said...

Yes, if they want to have countries in up alphabetical order, but with 'US' first, they will ask

ORDER BY country = 'US' desc, country

but they have to understand that the order is based on values and types of values. Expressions are values like others. and so make some tricky other stuff, like numeric values in alphabetical order or even worse use a stored procedure to give the order :D.

Richard Lynch said...

But in this case they *want* ORDER BY country = 'US' desc!
And sometimes you want the order by value as well.
A sort by distance routine that displays the miles for closest geo-coded marker, for example.