WPDB If primary key already exists, add +1 to integer field

This ended up being the correct formula in my case: $sql = “INSERT INTO foodWishes(foodWishName,foodWishPoints) VALUES (%s,%d) ON DUPLICATE KEY UPDATE foodWishPoints = foodWishPoints +1”; // var_dump($sql); // debug $sql = $wpdb->prepare($sql,$sanitized_foodWish,$foodWishPoint,); // var_dump($sql); // debug $wpdb->query($sql); if($sql){ //return the response echo $sql; }else{ echo “something went very very wrong”; } exit();

wpdb get_results() and prepare when to use prepare?

so if i have a function that gets terms from the database ( not the user ) do I need to use prepare first ( before get_results() ), or some sort of data sanitizing? Yes, but you should be using get_terms/WP_Term_Query/wp_get_object_terms/etc and the other term APIs instead as they’re safer and can be much faster. … Read more

Modify the structure of data returned by $wpdb

You can do this one of two ways, as far as I can see: Use PHP to combine the current results to the one you want. Basically: $records = array(); foreach( $results as $result ) { $record_id = $result[‘item_id’]; if (empty($records[$record_id])) { $records[$record_id][‘item_id’] = $result[‘item_id’]; $records[$record_id][‘item_name’] = $result[‘item_name’]; $records[$record_id][‘sub_items’] = array(); } $records[$record_id][‘sub_items’][] = array( … Read more

Get published posts and pages?

Don’t use pure SQL if you don’t have to. WordPress provides a useful and relatively solid class for retrieving post data. Use it. $args = array( ‘post_type’ => array(‘post’,’page’), ‘post_status’ => ‘publish’, ‘posts_per_page’ => -1, ‘ignore_sticky_posts’ => true, ); $qry = new WP_Query($args); // Show post titles foreach ($qry->posts as $p) { echo $p->post_title; } … Read more

wpdb->insert and stripslashes against sql injection

If you check out the source for the $wpdb->insert( $table, $data, $format) method you will find this comment: Data to insert (in column => value pairs). Both $data columns and $data values should be “raw” (neither should be SQL escaped). so you shouldn’t need to do the SQL escape yourself on the data. As far … Read more

What is _transient_random_seed for?

The wp_rand function mixes up the randomization of random numbers with various means, and in between runs it stores the random seed so as to keep the shuffling going every run. The random_seed transient is where it stores that.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)