The wpdb class, accessed via the $wpdb global variable, is used to interact with the WordPress database

learn more… | top users | synonyms

3
votes
3answers
2k views

SELECT max(meta_value) FROM wp_postmeta WHERE meta_key='price'… stops working when value is over 999

Overview: I am not trying to return a post. I simply want the single highest value for a particular meta_value across all posts... just the value itself. Details: I have added a custom meta_key ...
0
votes
1answer
172 views

$wpdb->prepare with ON DUPLICATE KEY UPDATE

I just took this code from $wpdb Codex page, and modified my existing Insert query: $table = $wpdb->prefix . 'user_req'; $post_id = explode('_', $_POST['post_id']); $user_id = $_POST['user_id']; ...
0
votes
0answers
59 views

Get thumbnail using wpdb from external wordpress database

I'm trying to get thumbnails from posts using a wpdb query where the wordpress database I'm querying is not the one from which the wpdb query is being called. That may be a little unclear, so here is ...
2
votes
2answers
255 views

$wpdb won't insert NULL into table column

When i try something like this $status = NULL; $wpdb->update( 'table', array( 'status' => $status, ), array( 'id' ...
1
vote
2answers
291 views

What is wrong with this wpdb update?

I've read a lot of documentation tonight, can't see why this isn't working. Obviously something I'm missing. $newstr = "18,19"; $defid = 1; $table = "wp_tablename"; $data = "array( 'somecol' => ...
0
votes
1answer
32 views

wpdb->update error

I am trying to update the value of an entry in the gravity forms detail table. I am simply trying to update the 'date' field inside of an entry. Here is my query: $update_date = ...
0
votes
1answer
27 views

Cannot update custom database table row

I am trying to update a row in a custom database table by $wpdb->update();. In my opinion my code is right but for some reason my database row doesn't update. Can anyone help me with this? I want ...
0
votes
1answer
34 views

How do I count columns on a custom WPDB query?

How would I count the columns of a custom WPDB query? This does work although I need a different kind of output: $sql_assoc = "SELECT * FROM test"; $num_cols = count((array) current($sql_assoc)); ...
3
votes
1answer
44 views

How to use mysql IN statement with wpdb update method?

I am using wpdb update method and wish to update records where the id is not found within multiple ids. So if I wanted to update records where each had an id of 1 I would do for example: ...
1
vote
1answer
38 views

Why does dbDelta() not catch MysqlErrors?

From what I can see, dbDelta() is designed to suppress database errors that occur during its operation. Generally speaking, this seems to be the case, but New Relic is still reporting MysqlErrors ...
1
vote
1answer
3k views

$wpdb->insert - inserting multiple rows

How do I use $wpdb->insert to insert multiple rows. Here is my code. for($i=0; $i<=$urlCount; $i++) { $stat = $wpdb->insert( 'WP_URLS', array( 'POSTID' => ...
2
votes
1answer
54 views

Using $wpdb Class to Pull Recent Comments Across a Network

Been looking for a clean and simple way to produce comments from across a network using the $wpdb database class. I did find this answer, which has been protected, and is four years old. I wrapped ...
1
vote
1answer
34 views

Detecting errors generated by $wpdb->get_results()

How do I detect errors when using $wpdb->get_results()? For example: $result = $wpdb->get_results("SELECT * FROM this is not a valid query"); The preceding code doesn't generate any exceptions ...
1
vote
1answer
113 views

Wordpress database error - Error in SQL syntax - I can't identify any error?

I am trying to write function that checks whether an email address is stored in my database or not. I've written the following PHP code:: function check_my_users( $email_address ) { global ...
0
votes
1answer
25 views

$wpdb prepare issue with mysql DATE_FORMAT

Today, I have a issue in $wpdb. I used $result = $wpdb->get_var( $wpdb->prepare( "SELECT DATE_FORMAT(report_date, '%d-%m-%Y') FROM table WHERE report_id = %d", $report_id ) ); ...
17
votes
6answers
19k views

Using wpdb to connect to a separate database

I want to connect wpdb to another database. How do I create the instance and pass it the database name/username/password? Thanks
1
vote
4answers
3k views

Export wordpress table to excel

I have created PHP scripts before to export a database table to .xls format like this: $select = "SELECT * FROM tracking"; $export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( ) ...
1
vote
1answer
44 views

Access Wordpress database outside of Wordpress

MAIN QUESTION: I am writing a wp plugin. Within it, I have a page located here: http://example.com/wordpress/wp-content/plugins/myplugin/page.php How do I access WP database from this page? Is ...
2
votes
1answer
101 views

Is $wpdb->prepare escaping to much? How to use it properly?

Im pretty much confused by $wpdb->prepare. I want to use it to sanitize user input but it turned out that it destroys the query. I cannot imagine that this is how it's intended to work but I can't ...
1
vote
1answer
31 views

I want to select the from values from database in WordPress? [closed]

Here is my code: $sql = 'SELECT * FROM ' . $wpdb->base_prefix . 'item WHERE uname = "'. $_POST['login_name'] . '" '; $result = $wpdb->get_results($sql) or die(mysql_error()); foreach($result as ...
1
vote
2answers
425 views

create drop down menu in theme customizer from custom db

what i am trying to do is create drop down menu in theme customizer from custom db i have created. db name "students". in the db i have only the id and the name. i want to show all students names in ...
0
votes
0answers
55 views

There is way to improve this query? wordpress get post data and some meta data

Suppose we have 2 blog with different database A and B. I want to get some post from Blog B to show them on Blog A. The problem comes when the posts have much metadata (custom field for example) that ...
1
vote
0answers
173 views

How to display users with posts published between two dates (Sorted by Post-Count) [Multisite]

I'm trying to make a list of users who have published posts within two given dates. My attempts using WP_User_query just work for one blog ID and takes time to load because it runs through a lot of ...
1
vote
1answer
147 views

wpdb->insert and stripslashes against sql injection

I've been reading the codex at this page: http://codex.wordpress.org/Function_Reference/stripslashes_deep The code i wrote before reading that page is this: $data = array( 'col1' => ...
1
vote
1answer
1k views

How to create database table, add data, update and delete using wpdb via plugins?

I am just starting out to create plugins with database. I know to create a custom database when activating the plugin. I just want to know whether we can create tables at the front end like adding the ...
2
votes
1answer
716 views

Problem in creating table through plugin code

I'm using the following code to create the database table using plugin. However, the table is neither getting created nor I'm getting any error message. The code is as follows: global $wpdb; ...
3
votes
2answers
1k views

Does $wpdb->prepare not create a prepared statement that I can execute multiple times?

I am wondering from the syntax $sql = $wpdb->prepare( 'query' [, value_parameter, value_parameter ... ] ) It appears (from the syntax - values within prepare) that it simple escapes values not ...
0
votes
1answer
98 views

Use wpdb->prepare for `order by` column name

In my current plugin, I am picking up the order by column as user input. So I thought of using wpdb->prepare to escape it. $wpdb->get_results($wpdb->prepare("Select id from $wpdb->posts ...
0
votes
1answer
44 views

Is there a WP function to get taxonomy name from taxonomy_term_id?

I need to get the taxonomy name from the taxonomy_term_id. I am currently using the following to get it: global $wpdb; $term_taxonomy_table = $wpdb->prefix . "term_taxonomy"; $taxonomy = ...
0
votes
0answers
41 views

Run query once inside a loop

I am trying to prevent to much queries inside a plugin one of my plugins. I have a query inside a function which will output a complete block with some layout elements and an input element which will ...
1
vote
2answers
40 views

How to use IN array properly in Wordpress?

I have an array containting these values (including the apostrohpes): 'course_3202','course_3201','course_3200','course_3199' I try to fetch results from these values in postmeta-table. SELECT * ...
1
vote
1answer
17 views

get the number of queries made on a page(inside my plugin)

Is there an easy way to see how many queries you make on a page(inside my plugin)? As I dont want to many queries in my plugin.
0
votes
1answer
44 views

how to get db values without using an loop with wpdb->get_results()

So I want to return the data from 1 row from my custom DB table, but I dont want to use a loop as I only need the values once....so is there a way to do this? $results = ...
1
vote
1answer
348 views

how to use $wpdb->prepare to update a custom table

As a noob I dont understand a lot of SQL injections but I need to be save so I have read that I have to use wpdb->prepare to make sure the data is stored correct. At this moment I use the ...
0
votes
0answers
49 views

Best way to execute custom page-specific mysql queries

I would like to query a secondary mysql database (not the Wordpress mysql database) with page-specific queries. I've seen many answers / tutorials on setting up a secondary mysql database, like: ...
0
votes
2answers
50 views

wpdb prepare: passing varible number of fields as second argument

As i want to manage the NULL fields in my db and wordpress functions doesn't allow to do so, i will need to dynamically generate a query depending on the situation. The problem is that i don't know ...
0
votes
2answers
289 views

Inserting data into `post meta` table?

How would I be able to insert data into the post meta table? I know how querying and wpdb works but I have no idea how I can just insert it into the table. I have these two fields I want to insert ID ...
2
votes
3answers
1k views

Does a query executed through wpdb class get cached?

I am not sure how WordPress cache the queries. I had the impression that whenever I execute a query through wpdb class, it gets cached. For example, on codex under Select a Row and Select a Var, it ...
0
votes
1answer
14 views

looking for a way to allow users to backup the plugin db data(save as)

I have a custom build plugin and I want to add the option to let the user save the plugin data(not using a thirdparty plugin to do this for me). So if a user clicks on the button it will allow them ...
1
vote
2answers
28 views

Get published posts and pages?

I'm working on a plugin to use gettext in posts and pages content. I'm trying to get all published posts and pages in order scan them for translatable strings, but I cannot figure out how to do it. ...
1
vote
1answer
181 views

wpdb update add current timestamp not working

So using the $wpdb->update to add some data to a custom table, trying to add the current timestamp but it's not saving the right stuff(0000-00-00 00:00:00 this is saved). overview code ...
0
votes
1answer
43 views

Create new database through static page code

I want to create a new table in current database of word press through create a static page. I write code for this as below, but there was no result. Why is that, and how can I get this right? ...
0
votes
1answer
97 views

How to access the WordPress DB from a plugin file

I'm building a basic video management plugin, with an embed feature that will have URLs like: mysite.com/wp-content/plugins/embed-video.php?video=[POST ID] I plan on using the [POST ID] url parameter ...
1
vote
1answer
83 views

Rolling Back from current WordPress version to previous one

What happens if we rollback from (for example) 3.8 to 3.5.1 and keep the same new DB created by the current version? Will that break anything?
0
votes
1answer
66 views

WP MS: How to query over the network

I have a WP 3.8.1 Multisite installation with 3 blogs, they're all in one db, prefixed wp_, wp_2_ and wp_3_. In a template in wp_ I try to to display all posts from wp_, wp_2_ and wp_3_ which have a ...
0
votes
2answers
115 views

Undefined variable: row?

I'm trying to select the post_title from wp_posts with a query which I did using this method: global $wpdb; $test = $wpdb->get_var( $wpdb->prepare(" SELECT post_title FROM ...
1
vote
2answers
704 views

How to write: $wpdb->update having WHERE NOT value pair in the array

I need to update all meta keys in the postmeta table that have a value other than 0, to the value of 0. I tried this.. no love (though I cannot figure out why....): $status = $wpdb->query("UPDATE ...
0
votes
2answers
64 views

Short of raw SQL, can I query for multiple attachment metadata that have a given array key?

With raw SQL through $wpdb, I believe that I could do something along the lines of this (untested): SELECT post_id, meta_value FROM postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value ...
0
votes
0answers
31 views

Proper way to trigger a MySQL query via link in a plugin

I am trying to develop a module in my WordPress plugin that will delete an entry from the database. The only issue is related to the trigger action. I've tried to send my users directly to the page ...
1
vote
1answer
24 views

Super administrator db name

As I cant test this(cant find any docs and dont have an multisite setup) I need to know the db name of the Super Administrator. So how is this role stored inside the wp db? It's probably something ...