Last active
November 6, 2022 11:05
-
-
Save EricBusch/54e89e07f87232fb055121bb766743fe to your computer and use it in GitHub Desktop.
A function for WordPress to check if a column exists in a database table or not. Returns true if a database table column exists. Otherwise returns false.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Returns true if a database table column exists. Otherwise returns false. | |
* | |
* @link http://stackoverflow.com/a/5943905/2489248 | |
* @global wpdb $wpdb | |
* | |
* @param string $table_name Name of table we will check for column existence. | |
* @param string $column_name Name of column we are checking for. | |
* | |
* @return boolean True if column exists. Else returns false. | |
*/ | |
function mycode_table_column_exists( $table_name, $column_name ) { | |
global $wpdb; | |
$column = $wpdb->get_results( $wpdb->prepare( | |
"SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s AND COLUMN_NAME = %s ", | |
DB_NAME, $table_name, $column_name | |
) ); | |
if ( ! empty( $column ) ) { | |
return true; | |
} | |
return false; | |
} |
Thanks, very helpful
It's very helpful,
if I want to insert data from same page where should I write code if(isset($_POST['submit']))
I would grateful having your solution
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
is there any faster query?