I tried this in local, and I think you have multiple fields inside $fields
so I’ve added them in array.
Just look at the code below; it works fine as tested:
add_action('your_hook', 'createTableFromFields');
function createTableFromFields($tablename)
{
$wpdb = $this->db;
$tablename = $wpdb->prefix . $tablename;
$fields = array('PersonID','LastName');
$sql="CREATE TABLE IF NOT EXISTS %s (id INT(6) UNSIGNED
AUTO_INCREMENT PRIMARY KEY";
$test = array();
foreach ($fields as $field) {
$test[] = $field." TEXT";
}
$t = implode(",", $test);
$sql .= ",%s)";
$result = $wpdb->query($wpdb->prepare(sprintf($sql, $tablename, $t)));
return $result;
}
Related Posts:
- Inserting Post Meta From SQL
- How to execute mulitple statement SQL queries using $wpdb->query?
- wpdb_prepare with multiple or condition
- Using wpdb to connect to a separate database
- WPDB Insert or if exists Update
- How to fetch Data in WordPress using MySQLi or $wpdb
- wpdb->insert multiple record at once
- How to define composite keys with dbDelta()
- Display data from a non wordpress database on a page template
- Why does dbDelta() not catch MysqlErrors?
- What causes the “max_user_connections” warning on WordPress frontend?
- Delete duplicate rows from wordpress database where a column is duplicate in phpmyadmin
- Database slowdown after update to 3.4.1
- Query WordPress database by registered date and role
- Size article thumbnails and retrieve them directly from database
- How to query the WordPress database to get posts of a certain custom post type, taxonomy and field?
- SQL Error wp_commentmeta – incorrect table definition
- WordPress database error – Error in SQL syntax – I can’t identify any error?
- WordPress and MySQL: how to transfer Meta_key and Meta_Value from one post_id to another
- How do I get the posts within a certain year/date(with sql query)?
- Advanced SELECT query with condtional statements
- Redirecting to old domain after migration website
- WordPress running SQL query to update database from form
- WPDB SQL query with prepare() returning variable, not db value
- Have working sql query… trying to adjust it to use $wpdb
- Custom WordPress Table wpdb
- Why this query is not showing any result on wordpresss home page?
- Best practice to limit results in get_row()?
- Clear Terms from Taxonomy for Specific Post IDs?
- Why does changing Database Table Prefixes result in loss of content?
- Trouble running $wpdb->query() with last_insert_id
- SQL query to set posts in bulk based on the post content
- Query Column of Specific ID from Database Table
- How to delete a particular row in a database table
- How to create more than one new wpdb object?
- How to connect and insert data in database of wordpress?
- $wpdb->get_var – What value is returned when zero records are found?
- Getting value from database table depending on field value
- How to escape percentage sign(%) in sql query with $wpdb->prepare?
- DB prefix not updating
- I wanted to Know that how can I give ingore to 0000-00-00 in my config file
- “BS_” rows in postmeta table
- Where is the HTML-handler part in the wpdb class?
- Help posting values to DB on submit using $wpdb->query
- WordPress Database Query works in phpMyAdmin but not in the code
- How to create index (sql) to a meta_key?
- 2 $wpdb queries causing error Table ‘wp_postmeta’ is specified twice, both as a target for ‘UPDATE’
- Delete all users meta that named: user_avatar
- insert data from a form:: Warning Empty query mysqli::query()
- How can I get $wpdb to show MySQL warnings?
- Correct and secure way to access a custom SQL database in a custom PHP template file
- How to get specific table by current user login
- The MySQL Connection could not be established., before it was joomla but i have change it into worpress
- How To connect to the same WordPress database with different database user
- using same mysql user with many databases
- Migrated to Namecheap, now Trouble with Database & wp-config [closed]
- export individual posts to text files or a single csv file
- SQL query : Select a thumbnail for a media
- why you don’t Implement PDO for access every thing to database
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- Uploading to WordPress Database
- #1067 – Invalid default value for ‘post_date’ when trying to create a new column
- Deleting WordPress posts and media files based on author via MYSQL database
- SQL errors when querying for something with apostrophes
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- MySQL Error: : ‘Access denied for user ‘root’@’localhost’
- Rename a table in MySQL
- How should I tackle –secure-file-priv in MySQL?
- What is “Advanced” SQL?
- Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
- Case statement in MySQL
- Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
- Can’t connect to MySQL server on ‘127.0.0.1’ (10061) (2003)
- How do I restore a dump file from mysqldump?
- MySQL SELECT increment counter
- Error Code: 2013. Lost connection to MySQL server during query
- how to drop partition without dropping data in MySQL?
- $wpdb->prepare returns empty array
- How to delete field using WPDB?
- PHP Warning: Error while sending QUERY packet
- Get all the related data from WordPress DB
- Importing Concrete5 content to WordPress
- Custom wordpress SQL statement for a website
- SQL query to rewrite all media URLs to end with .webp
- Weird WP -Cli Error Connection Refused
- WordPress database connection failed, while mysql client is working [closed]
- How to find fresh (last modified) and sql backup?
- Have WordPress Use a Different Database for users
- Putting form result in my database
- $wpdb->update is not working until next page refresh. Is there one more step?
- How to generate PDF dynamically based on values from the database in WordPress
- Understanding Redundant postmeta Tables in WordPress Database
- how to run tests using different db connection
- How to properly prepare a column name if passed to a stored procedure?
- Database table prefix different between wp-config.php and in database
- Naming conventions for database tables
- WordPress User Login not accepted although listed in DB
- Count views without get_post_meta
- Problem while importing a site in WPLocal: SQL files fail to import