global variable not working inside function - php

In a wordpress plugin I have this code
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $wpdb;
global $country_table;
global $cities_table;
$AO_name = "trackbyid_dbv";
$AO_value = "1";
add_option($AO_name, $AO_value);
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $country_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
country tinytext NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ( ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
$sql = "CREATE TABLE $cities_table(
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
city tinytext NOT NULL,
country_id mediumint(9) NOT NULL,
city_latitude mediumint(9) NOT NULL,
city_longitude mediumint(9) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
dbDelta($sql);
}
I think my problem (Database is not being created) is with global $country_table and $cities_table variables. when I make that variables as local the problem solves!
whats wrong with my global variable declaration and usage?

Your variables $country_table and $cities_table won't be defined because you didn't bring in the global $wpdb before you tried to use it in those vars. Try this instead:
global $wpdb;
$country_table = $wpdb->prefix . "trackbyid_country";
$cities_table = $wpdb->prefix . "trackbyid_cities";
function database_creation(){
global $country_table, $cities_table;
...
}

Related

Cannot create table in wordpress

I cannot create table in wordpress, i'm not understand why?
private function create($prefix)
{
$sql = "CREATE TABLE " . $prefix . "submit (
submit_id bigint PRIMARY KEY AUTO_INCREMENT,
post_id bigint,
user_id bigint,
author text,
user_email text,
source text,
pass text,
language text,
time datetime,
CONSTRAINT post_id FOREIGN KEY (post_id) REFERENCES wp_posts(ID),
CONSTRAINT user_id FOREIGN KEY (user_id) REFERENCES wp_users(ID)
)";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
var_dump(dbDelta($sql));
}
The var_dump() shows:
array(1) { ["wp_submit"]=> string(23) "Created table wp_submit" }
add_action("after_switch_theme", "my_custom_table_setup");
function my_custom_table_setup()
{
global $wpdb;
$create_table_query = "
CREATE TABLE {$wpdb->prefix}products_by_sku (
id int(11) NOT NULL auto_increment,
product_id int(11) NOT NULL,
lot_id varchar(25) NOT NULL,
title varchar(255) NOT NULL,
LP varchar(25) NOT NULL,
catgeory varchar(255) NOT NULL,
product_name varchar(255) NOT NULL,
UPC varchar(255) DEFAULT NULL,
ASIN varchar(255) DEFAULT NULL,
original_retail_price float DEFAULT NULL,
quantity int(11) DEFAULT NULL,
total_original_retail float NOT NULL DEFAULT 0,
stock_image text,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $create_table_query );
}
Try this code.
Thank you.
You can use this code:
global $jal_db_version;
$jal_db_version = '1.0';
function jal_install() {
global $wpdb;
global $jal_db_version;
$table_name = $wpdb->prefix . 'liveshoutbox';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
name tinytext NOT NULL,
text text NOT NULL,
url varchar(55) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
add_option( 'jal_db_version', $jal_db_version );
}
register_activation_hook( __FILE__, 'jal_install' );

Wordpress plugin activation creating table

I am trying to create a table with my wordpress plugin on activation. I have this and it is not working:
function activate_cron() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$table_name = $wpdb->prefix . "fbcron";
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once( '../../../wp-admin/includes/upgrade.php' );
dbDelta($sql);
}
register_activation_hook( __FILE__, 'activate_cron' );
I believe for dbDelta the collate needs to be on its own line. It can be easier to make sure you have the right spacing by embedding the variables in the string:
$sql = "CREATE TABLE {$table_name} (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
)
{$charset_collate};";
I think you have a problem with wp-admin/includes/upgrade.php path
I improve your existing code, tested
function activate_cron() {
global $wpdb;
$table_name = $wpdb->prefix . 'fbcron';
//check if table exist
if($wpdb->get_var("SHOW TABLES LIKE '$table_name';") != $table_name) {
$sql = "CREATE TABLE ".$table_name." (
id int(11) NOT NULL AUTO_INCREMENT,
post_id int(11) NOT NULL,
PRIMARY KEY (id)
) ".$charset_collate.";";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); //this path
dbDelta($sql);
}
}
register_activation_hook( __FILE__, 'activate_cron' );
Hope it helps

Unable to create a table in existing wordpress database

I want to create a table in existing wordpress database and I am unable to create it. I have write this code inside a custom plugin files. Please help me through this code.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
register_activation_hook(__FILE__, 'create_xml_table');
}
You have to do like this.
function create_xml_table(){
global $wpdb;
$tableName = $wpdb->prefix."XMLdata";
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $tableName(id int(10) NOT NULL AUTO_INCREMENT,
user_id varchar(255) NOT NULL,
post_id varchar(255) NOT NULL,
value varchar(255) DEFAULT '' NOT NULL,
time timestamp ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
)$charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
register_activation_hook(__FILE__, 'create_xml_table');

Wordpress table not being created

I have been trying to create a new table for my wordpress plugin for a few hours now without success.
My best guess is that there is some error in the sql which i cannot find. I have been trying to look at the wordpress codex and other sources as well without success. Would apprechiate some help!
register_activation_hook( __FILE__, 'plugin_install' );
function plugin_install(){
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;
$table_name = $wpdb->prefix . 'dbname';
$charset_collate = $wpdb->get_charset_collate();
$sql_create_table = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL PRIMARY KEY AUTO_INCREMENT,
b_id int(11) NOT NULL,
iprefix varchar(20) NOT NULL default '0',
istart bigint(20) NOT NULL default 'updated',
iend bigint(20) unsigned NOT NULL default '0',
isuffix varchar(20) NOT NULL default 'post',
PRIMARY KEY (id)
) $charset_collate; ";
dbDelta( $sql_create_table );
}
In the fourth line of your create SQL code you have an unsigned varchar. Remove the unsigned keyword and it should be fine.

Wordpress not creating database table on activitation

$table_name = $wpdb->prefix . 'offline_card';
// function to create the DB / Options / Defaults
function offline_card_install() {
global $wpdb;
global $table_name;
// create the ECPT metabox database table
if($wpdb->get_var("show tables like '$table_name'") != $table_name)
{
$sql = "CREATE TABLE $table_name (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`order_id` int NOT NULL,
`card_number` varchar(55) NOT NULL,
`card_expiry` varchar(55) NOT NULL,
`card_ccv` varchar(22) NOT NULL,
UNIQUE KEY id (id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
}
// run the install scripts upon plugin activation
register_activation_hook(__FILE__,'offline_card_install');
I don't understand what is the reason that it is not creating database table on plugin activation. It is not showing any error though.
Your code looks okay. Might be issue with your variable $table_name.Write it into function and try once:
function offline_card_install() {
global $wpdb;
$table_name = $wpdb->prefix . 'offline_card';
Have you checked $table_name variable returns anything?

Categories