check_column

Check column matches criteria.

Description

check_column( (string) $table_name, (string) $col_name, (string) $col_type, (constant) $is_null = null, (constant) $key = null, (constant) $default = null, (constant) $extra = null ); 

Uses the SQL DESC for retrieving the table info for the column. It will help understand the parameters, if you do more research on what column information is returned by the SQL statement. Pass in null to skip checking that criteria.

Column names returned from DESC table are case sensitive and are listed: Field Type Null Key Default Extra

Parameters (7)

0. $table_name (string)
The table name.
1. $col_name (string)
The col name.
2. $col_type (string)
The col type.
3. $is_null — Optional. (constant) => null
Check is null.
4. $key — Optional. (constant) => null
Key info.
5. $default — Optional. (constant) => null
Default value.
6. $extra — Optional. (constant) => null
Extra value.

Usage

  1. if ( !function_exists( 'check_column' ) ) { 
  2. require_once ABSPATH . '/wp-admin/install-helper.php'; 
  3.  
  4. // The table name. 
  5. $table_name = ''; 
  6.  
  7. // The col name. 
  8. $col_name = ''; 
  9.  
  10. // The col type. 
  11. $col_type = ''; 
  12.  
  13. // Optional. Check is null. 
  14. $is_null = null; 
  15.  
  16. // Optional. Key info. 
  17. $key = null; 
  18.  
  19. // Optional. Default value. 
  20. $default = null; 
  21.  
  22. // Optional. Extra value. 
  23. $extra = null; 
  24.  
  25. // NOTICE! Understand what this does before running. 
  26. $result = check_column($table_name, $col_name, $col_type, $is_null, $key, $default, $extra); 
  27.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/install-helper.php  
  1. function check_column($table_name, $col_name, $col_type, $is_null = null, $key = null, $default = null, $extra = null) { 
  2. global $wpdb; 
  3. $diffs = 0; 
  4. $results = $wpdb->get_results("DESC $table_name"); 
  5.  
  6. foreach ($results as $row ) { 
  7.  
  8. if ($row->Field == $col_name) { 
  9.  
  10. // Got our column, check the params. 
  11. if (($col_type != null) && ($row->Type != $col_type)) { 
  12. ++$diffs; 
  13. if (($is_null != null) && ($row->Null != $is_null)) { 
  14. ++$diffs; 
  15. if (($key != null) && ($row->Key != $key)) { 
  16. ++$diffs; 
  17. if (($default != null) && ($row->Default != $default)) { 
  18. ++$diffs; 
  19. if (($extra != null) && ($row->Extra != $extra)) { 
  20. ++$diffs; 
  21. if ($diffs > 0) { 
  22. return false; 
  23. return true; 
  24. } // end if found our column 
  25. return false;