create table in wordpress using functions.php

By using this code you can create table in sql and if you want to insert data than use wp insert…

<?php
global $wpdb;

$appointment_table_name = $wpdb->prefix . “book_an_appointment”;
if($wpdb->get_var(“show tables like ‘$appointment_table_name'”) != $appointment_table_name) {

$sql = “CREATE TABLE ” . $appointment_table_name . ” (
id mediumint(9) NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
phone VARCHAR(25) NOT NULL,
date VARCHAR(25) NOT NULL,
time VARCHAR(25) NOT NULL,
comment VARCHAR(1500) NOT NULL,
UNIQUE KEY id (id)
);”;

require_once(ABSPATH . ‘wp-admin/includes/upgrade.php’);
dbDelta($sql);

}
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s