$tablename = $wpdb->prefix . "name";
$sql = "CREATE TABLE IF NOT EXISTS $tablename(
time_id INT(11) NOT NULL AUTO_INCREMENT,
job VARCHAR(120) NOT NULL,
employee VARCHAR(100) NOT NULL,
date_entry DATETIME NOT NULL,
duration INT(11) NOT NULL,
PRIMARY KEY(time_id)
);";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
creates two tables: 'wp_name' and 'name'. If I define $tablename = 'wp_name' instead it only creates one table. Also '{$wpdb->prefix}name' doesn't change the problem.
What's the problem here? - thanks!
Get rd of the brackets
$sql = "CREATE TABLE IF NOT EXISTS $tablename (
...
);";
They are only used to replace internal wp variables
Update:
your case can't create two tables by itself, there is no way, you have somewhere the same code twice.
Without the prefix, the second code checks that there is already a table named nameand does nothing, but when you add the prefix, it tries to add the tbale name which azt this time doesn't exist, because it is call wp_name
Related
I have a table in database naming "Customers". The table have total 83 columns. I want to create another "NewCustomers" table like "Customers" using php and mysql query. If the "NewCustomers" table exists in the database then it is no need to create the table if it is not then it will create the "NewCustomers" table.
I know there is a query
CREATE TABLE 'NewCustomers' LIKE 'Customers';
But it only creates the table like Customers if the New Customers not exists. How can I use
IF NOT EXISTS
in this regard?? I don't want to write all 83 columns name again like
CREATE TABLE IF NOT EXISTS `NewCustomers` (
`id` int(11) NOT NULL auto_increment,
'name` varchar(250) NOT NULL,
`data` varchar(100) NOT NULL default '',
.
.
.
PRIMARY KEY (`id`)
);
simple do like this
CREATE TABLE IF NOT EXISTS `NewCustomers` like `Customers`
Note :
And also it's safe to refer the DB name like below
CREATE TABLE IF NOT EXISTS `NewCustomers` like `DB_name`.`Customers`
I am working on a project, and I have to use sql. The variable $file_name needs to be the table name, but when i try this:
$sqlTableCreate = "CREATE TABLE ". $file_name . "(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
The table does not create. I checked by using this:
if ($sqlConnection->query($sqlTableCreate) === TRUE) {
echo 'Created Sucessfully';
} else {
echo 'Table does not create.';
}
I get 'Table does not create' when trying to use this. Help would be greatly appreciated. Thanks in advance!
Your filename contains a extension, but I suspect you just want to use the name without the extension as the name of the table. You can use the basename function to remove the extension.
$sqlTableCreate = "CREATE TABLE ". basename($file_name, ".csv") . "(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
If there can be different extensions, and you want to remove them more generally, see
How to remove extension from string (only real extension!)
I don't see any issue with your posted query but couple things may be wrong
Make sure that there is no table exists with that same name. You can use IF NOT EXISTS marker to be sure like
CREATE TABLE IF NOT EXISTS". $file_name . "(
make sure that the variable $file_name is not empty. Else, you are passing a null identifier in CREATE TABLE statement; which will not succeed.
Per your comment: you have $file_name = 'currentScan.csv';
That's the problem here. You are trying to create a table named currentScan.csv which your DB engine thinking that currentscan is the DB name and .csv is the table name which obviously doesn't exits and so the error.
first check your database connection and change your query with given below :
$sqlTableCreate = "CREATE TABLE ". $file_name . " (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";
So I want to create a new table in my database and I can't seem to get it working. It's a wordpress plugin so I don't know if that's what could be messing it up.
if ($wpdb->get_var('SHOW TABLES LIKE "' . $wpdb->prefix . DB_PROFILE_TABLE . '"') != $wpdb->prefix . DB_PROFILE_TABLE)
{
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100)
CHARSET=utf8; ';
dbDelta($sql);
}
Beginner at php and mySql.
You are missing parenthesis...
Can you try in this way
global $wpdb;
$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,
UNIQUE KEY id (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
These are the rules to create table using wordpress plugin
1) You must put each field on its own line in your SQL statement.
2) You must have two spaces between the words PRIMARY KEY and the definition of your primary key.
3) You must use the key word KEY rather than its synonym INDEX and you must include at least one KEY.
4) You must not use any apostrophes or backticks around field names.
Field types must be all lowercase.
5) SQL keywords, like CREATE TABLE and UPDATE, must be uppercase.
Refer http://codex.wordpress.org/Creating_Tables_with_Plugins
Looks like you are missing a closing parenthesis after country VARCHAR(100). You may also need to remove the semi-colon as well.
$sql = 'CREATE TABLE ' . $wpdb->prefix . DB_PROFILE_TABLE . ' (
id BIGINT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
fname VARCHAR(100) NOT NULL,
sname VARCHAR(100),
city VARCHAR(100),
country VARCHAR(100))
CHARSET=utf8';
dbDelta($sql);
global $wpdb;
$table_name = $wpdb->prefix . "product_order";
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
range VARCHAR(255),
category VARCHAR(255),
order mediumint(9),
relation mediumint(9),
UNIQUE KEY id (id)
);";
$wpdb->query($sql);
That doesn't seem to create a table... any reasons why? I tried dbdelta too.
For the sake of an answer, surround the column names in back ticks as range and order are reserved words in MySQL:
$sql = "CREATE TABLE $table_name (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`range` VARCHAR(255),
`category` VARCHAR(255),
`order` mediumint(9),
`relation` mediumint(9),
UNIQUE KEY id (id)
);";
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
As a side note, if you want to make sure you don't conflict with other tables should this schema be shared with other wordpress instances or become a multisite, consider using {$wpdb->prefix}$table_name instead of just $table_name to use the database prefix defined in wp-config.php.
I am very new to WordPress, and writing a plugin to (ideally) allow our client to update their existing WP site and the dynamic info on our iphone app all in one place. This will require the creation of two tables. I have written what I believe should be creating these tables, but activating the plugin throws this error on my test WordPress site:
The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Looking into the database also reveals that the tables were not created. I have looked at a handful of other posts here and elsewhere, but can't seem to isolate where I am going wrong. Any suggestions?:
<?php
// Plugin info omitted
register_activation_hook(_FILE_, 'event_manager_install');
register_deactivation_hook(_FILE_, 'event_manager_uninstall');
// Installer
function event_manager_install() {
global $wpdb;
// Builds queries for custom event and speakers tables
$event_table = $wpdb->prefix . 'events';
$eventSql = "CREATE TABLE $event_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
venueName VARCHAR(250) DEFAULT '' NOT NULL,
date datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
address text NOT NULL,
registrationDeadlineDate datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
UNIQUE KEY id (id)
);";
$speaker_table = $wpdb->prefix . 'speakers';
$speakerSql = "CREATE TABLE $speaker_table (
id mediumint(9) NOT NULL AUTO_INCREMENT,
eventID mediumint(9) NOT NULL,
speaker text NOT NULL,
UNIQUE KEY id (id)
);";
// Executes queries
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($eventSql);
dbDelta($speakerSql);
}
// Uninstaller
function event_manager_uninstall() {
global $wpdb;
// Creates queries to delete custom tables
$event_table = $wpdb->prefix . 'events';
$speaker_table = $wpdb->prefix . 'speakers';
$eventSql = "DROP TABLE " . $event_table . ";";
$speakerSql = "DROP TABLE " . $speaker_table . ";";
// Executes deletion queries
$wpdb->query($eventSql);
$wpdb->query($speakerSql);
}
?>
The problem is that you've misspelled the name of the __FILE__ constant. Just replace _FILE_ with __FILE__ (note: two underscores), and it should work just fine.