Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

PDOException - SQLSTATE[22003] - Numeric value out of range

Parent Feed: 

This blog describes how to solve PDOException - SQLSTATE[22003] - Numeric value out of range: 1264 Out of range. When you try to store large integer value in 'integer' field type, then you will get this error. It is because the maximum value of 'integer' field type is exceeded. 

For example, you want to store phone number in a content type. Then you may create new field with 'integer' field type in the content type. When you store 10 digit phone number in this field, "PDOException error" will be shown. MySQL allows you to store values from -2147483648 to 2147483647 in integer field type if signed, so you can't store phone number in 'integer' field type. MySQL allocates 4 bytes for 'integer' field type ie.,

4 bytes = 4 x 8 = 32 bits
2^32 => 4294967296 values
2^32 - 1 => 4294967295 (if unsigned, 0 to 4294967295 values)
-2^(32 - 1) to (2^(32 - 1) - 1) => -2147483648 to 2147483647 (if signed, -2147483648 to 2147483647 values).

If you want to store large integer value, then you need to use field type either 'bigint' or 'text field'. If you choose 'bigint', then there is no need to add custom validation for phone number field. On the other hand, if you choose 'text field' then you need to add custom validation using either hook_form_alter() or hook_form_form_id_alter().

<?php
/**
 * Implement hook_form_alter()
 */
function kf_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    // add custom validate handler
    $form['#validate'][] = 'kf_test_form_validate';
  }
}

/**
 * Implement kf_test_form_validate
 */
function kf_test_form_validate($form, &$form_state) {
  if (isset($form_state['values']['field_phone_number'][LANGUAGE_NONE][0]['value'])) {
    $phone = $form_state['values']['field_phone_number'][LANGUAGE_NONE][0]['value'];
    if (!ctype_digit($phone)) {
      form_set_error('field_phone_number', t('Please enter valid phone number..!'));
    }
  }
}
?>

The ctype_digit() is used to check whether all of the characters in a given string are numerical or not. So it doesn't allow user to store string values as phone number.

Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web