global $wpdb;
$db_name=$wpdb->someone;
$id=5;
$result =1;
$note='Test note';
$data=array(
'is_success'=>$result,
'note'=>$note,
'success_time'=>date("Y-m-d H:i:s"),
);
$where=array(
'id' => $id
);
$format = array(
'%d',
'%s',
'%s'
);
$where_formart = array(
'%d'
);
$ok = $wpdb->update($db_name,$data,$where,$format,$where_formart);
I want to update 3 fields,but it only update the 'success_time'
field,and $ok
is true,why?
PS: all fields to be updated allow null.
Did you know?
I found that using wpdb->update to set a db field to null is not working. I did some research and found the following info:
– https://core.trac.www.ads-software.com/ticket/15158
– https://www.ads-software.com/support/topic/bug-fix-wpdb-insert-amp-update-with-null-values?replies=7
Given the info in the ticket, it looks like it’s still a bug, even in the current version of wordpress.
An example of what I want to do is:
$c1 = "";
$c2 = NULL;
$wpdb->update(
'table',
array(
'column1' => $c1, // string
'column2' => $c2 // integer (number)
),
array( 'ID' => 1 ),
array(
'%s', // value1
'%d' // value2
),
array( '%d' )
);
Note – the db columns do allow NULL.
Is this still not possible?
Thanks.
]]>The following is not working at all:
<?php if($_POST['submit']) {
global $wpdb;
$preek_naam = $_POST['preek_naam'];
$preek_gemeente = $_POST['preek_gemeente'];
$preek_teks = $_POST['preek_teks'];
$wpdb->update('bible_afri', array('preke' => $preek_teks), array('index' => 1), array('%s'));
}
but if I replace array('preke' => $preek_teks)
with array(‘preke’ => “some text here”)` it loads it into the DB!
I have no idea why.
]]>After updating to 3.5.1 I have encountered some strange behavior in WordPress that I can’t explain. Let me give an example.
I use a custom post type, “my_post.” Previous to updating to 3.5.1, I used this code to update this post type’s status:
$result = $wpdb->update(
$wpdb->posts,
array( 'post_status' => $new_status ),
array( 'ID' => $id )
);
This no longer updates the row in the database, but if I check $result, it is equal to 1, indicating that 1 row was affected by $wpdb->update(). So I decided to check what query was being sent to the database by adding this line right below the above code:
exit( var_dump( $wpdb->last_query ) );
Sure enough, this is what was returned to me (where 11111 is just an example ID):
UPDATE wp_posts SET post_status = 'new_status' WHERE ID = 11111
However, once I checked the database, exiting immediately after using the $wpdb->update() function actually resulted in the database row being updated. I removed the exit() code and tried again using the same code, just to make sure I was observing this correctly. Without the exit() code, no update was made to the database even though $result still returned 1.
So then I gave up and decided to use this for updating my custom posts until I understand why $wpdb->update() is no longer working for me:
$my_post = array();
$my_post['ID'] = $id;
$my_post['post_status'] = $new_status;
wp_update_post($my_post);
This solution works, but I want to know why $wpdb->update() is no longer working. Does anyone have any ideas?
]]>On the submit.php file, I am trying to run a query to update some values in the table I built by using this:
$update = $wpdb->update( my_table, array ( guesses=>$numGuesses), array (id=>$id));
Although I have the “global $wpdb” defined, I still receive “Fatal error: Call to a member function get_results() on a non-object”. The widget successfully uses the $wpdb as it queries the same table to pull the data for the questions (uses $wpdb->get_results()). I don’t understand what I am doing wrong to make this not work, for I have looked over both files and nothing appears to be different.
Does anybody have any thoughts on what it could be?
]]>$wpdb->insert( $table_name, array( 'user_id' => $user_id, 'first_name' => $first_name ));
Then:
echo $user_id." | ".$wpdb->last_query;
Returns:
100003685270940 | INSERT INTO 'wp_table_name'(
user_id,
first_name) VALUES ('2147483647','Dave')
Notice that the $user_id printed by PHP and passed to the insert method is different from the one used by the method.
I’ve cleared the cache($wpdb->flush()) and also done a cache reload. What’s causing this? I have no caching plugins enabled
Any idea what might be wrong here?
$wpdb->update('wp_network_members', $insert_member_data WHERE 'f_name' = $insert_member_data['f_name'] AND 'l_name' = $insert_member_data['l_name']);
]]>