How to add new tables in WordPress with the same encoding as... open

How to add new tables in WordPress with the same encoding as the database. Code example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

This is a private static method in PHP that returns an array containing two SQL CREATE TABLE statements.

The first SQL statement creates a table named “user_action” with four columns: ID (integer), user (integer), time_action (datetime), and a primary key constraint on the ID column and a unique key constraint on the user column.

The second SQL statement creates a table named “temp_media” with five columns: media_id (integer), user_id (integer), session_id (string), uploader_id (string), and upload_date (datetime). The media_id column has a unique key constraint, while the upload_date column has an index defined on it. Both tables are created with the collation specified by the current WordPress instance’s database connection settings.

	private static function get_tables() {
		global $wpdb;

		$collate = '';

		if ( $wpdb->has_cap( 'collation' ) ) {
			if ( ! empty( $wpdb->charset ) ) {
				$collate .= "DEFAULT CHARACTER SET $wpdb->charset";
			}
			if ( ! empty( $wpdb->collate ) ) {
				$collate .= " COLLATE $wpdb->collate";
			}
		}

		return array(
			"
			CREATE TABLE IF NOT EXISTS `" . PREF . "user_action` (
				ID BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
				user BIGINT(20) UNSIGNED NOT NULL,
				time_action DATETIME NOT NULL,
				PRIMARY KEY  id (id),
				UNIQUE KEY user (user)
			) $collate",
			"CREATE TABLE IF NOT EXISTS `" . PREF . "temp_media` (
				media_id BIGINT(20) UNSIGNED NOT NULL,
				user_id BIGINT(20) UNSIGNED NOT NULL,
				session_id VARCHAR(200) NOT NULL,
				uploader_id VARCHAR(200) NOT NULL,
				upload_date DATETIME NOT NULL,
				UNIQUE KEY  media_id (media_id),
				KEY upload_date (upload_date)
			) $collate"
		);
	}
	
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation