Suppose I have a table in my wp database fruits:
CREATE TABLE `fruits` (
`fruit` varchar(99),
`istasty` tinyint(1)
)
INSERT INTO `fruits` (`fruit`, `istasty`) VALUES
('pear', 1),
('peach', 1),
('rotten strawberry', 1),
('strawberry', 1),
('banana', 1),
('apple', 1);
the goal is to update istasty to 0 in the 'rotten strawberry' row.
the class reference for wpdb in the wordpress codex gives:
$wpdb->update( $table, $data, $where, $format = null, $where_format = null );
so I've attempted the following:
$wpdb->update(
'fruits',
array(
'fruit' => 'rotten strawberry',
'istasty' => '0'
),
array( 'fruit' => 'rotten strawberry' ),
array(
'%s', // value1
'%d' // value2
),
array( '%d' )
);
Apparently this updates EVERY record to 'rotten strawberry', '1'.
What am I doing wrong here?
Try changing up that last array from array( '%d' ) into array( '%s' ). You are now telling the script that the where variable is an integer, which it is not.
Also you could remove 'fruit' => 'rotten strawberry' from your first array. In this array you place whatever you want to update. And all you want to do is set istasty to 0.
Related
I have a "usermeta" table in db.
and there are some columns including meta_key and meta_value.
the delivery_location(meta_key) has same meta_value.
so I want to replace current meta_value to new value in deliery_location
(70982, 20, 'delivery_location', 'a:61:{i:0;s:47:"{lat:32.89030473256227, lng:-96.96264851172401}";i:1;s:47:"{lat:32.89359300394015, lng:-96.94936752319336}";i:60;s:0:"";}'),
my question is that how I can replace the all same meta_values at once in mysql?
You can get all users using get_users(). check the below code. code goes in your active theme functions.php file.
function update_all_user_delivery_location(){
$users = get_users( array( 'fields' => array( 'ID' ) ) );
foreach ( $users as $key => $user ) {
$delivery_location = get_post_meta( $user->ID, 'delivery_location', true );
// modify your code here
update_post_meta( $user->ID, 'delivery_location', $delivery_location );
}
}
add_action( 'init', 'update_all_user_delivery_location', 10, 1 );
In short i want get meta values from usermeta table but the meta value is in serialize array form,
these values are post ids actually, this is my working code for single meta value, i want multiple values from serialize array in meta_value
$user_id = get_current_user_id();
$key = 'classes';
$single = true;
$user_last = get_user_meta( $user_id, $key, $single );
$user_last;
$query_args = array(
'posts_per_page' => $output,
'post_status' => 'publish',
'post_type' => 'stm-courses',
'meta_query' => array(
array(
'key' => 'classes',
'value' => $user_last,
'compare' => 'LIKE'
)
)
);
print_r( $query_args ); echo "string";
the single meta value is working fine but not multiple values
the below is the output of the above query
Array ( [posts_per_page] => 3 [post_status] => publish [post_type] => stm-courses [meta_query] => Array ( [0] => Array ( [key] => classes [value] => Array ( [0] => 5033 [1] => 5034 ) [compare] => LIKE ) ) ) string
and in database the value for meta_key classes is stored something like this
a:2:{i:0;s:4:"5033";i:1;s:4:"5034";}
the values are dynamically changeable so i need something dynamic logic,
Thanks in advance, pls suggest me any good idea how i do this
$meta_query[] = array(
'key' => 'classes',
'value' => sprintf('"%s"', $user_last),
'compare' => 'LIKE',
);
I don't think Jagan's solution will work.
I've the same problem and if i look in the database, a meta_key in a serialize array are in the row meta_value (of an other meta_key )... Logic.
So if you try to get
meta_key => "classes",
i think the result will be null.
If someone have a solution to get the value of a meta_key in a serialize array with a wp_query ? I take it !
How do I transform this
DB::table('partners')->insert(array($data));
laravel Query to have ON DUPLICATE KEY UPDATE
the structure look like
$data['program_name'] = $program['program']['_'];
$data['program_id'] = $program_id;
$data['status'] = $program['status'];
$data['shop_name'] = $shop->name;
$data['shop_logo'] = $shop->image;
$data['shop_description'] = $shop->description;
where program_id is unique
Use something like this (feel free to correct because it's untested):
DB::statement( 'INSERT INTO partners VALUES (' . implode( ',',
array_map( function( $val ) { return ":$val"; } , array_keys($data) )
) . ') ON DUPLICATE KEY UPDATE ' . implode( ',',
array_map( function( $val ) { return "$val = VALUES($val)"; } , array_keys($data) )
), $data);
I created a package that will wrapped INSERT ON DUPLICATE KEY UPDATE
https://packagist.org/packages/yadakhov/insert-on-duplicate-key
$users = [
['id' => 1, 'email' => 'user1#email.com', 'name' => 'User One'],
['id' => 2, 'email' => 'user2#email.com', 'name' => 'User Two'],
['id' => 3, 'email' => 'user3#email.com', 'name' => 'User Three'],
];
User::insertOnDuplicateKey($users);
// produces:
INSERT INTO `test_user_table`(`id`,`email`,`name`) VALUES
(1,'user1#email.com','User One'), (2,'user3#email.com','User Two'), (3,'user3email.com','User Three')
ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)
I want to convert following complex mysql into cake's find expression:
SELECT p1, p2
FROM
(
SELECT IFNULL(a.c2, '10') AS p1, IFNULL((SELECT MAX(c.c1) FROM my_table c WHERE c.c1>p1), '30') AS p2
FROM my_table a
WHERE
(
(a.user_id = 2) AND (a.c1 BETWEEN '10' AND '30')
)
) as temp
WHERE p2 > 100
ORDER BY p1;
I tried following
http://dogmatic69.com/sql-to-cakephp-find-converter
but unable to generate the desired expression.
Please help. I really don't know how to handle such complex expressions (I do not prefer to use query in cakephp)
Thanks
Let me just convert your query into the cakephp way
if table a's model is A:
$fields = "IFNULL(A.c2, '10') AS p1, IFNULL((SELECT MAX(C.c1) FROM my_table c WHERE C.c1>p1), '30') AS p2";
$conditions = "A.user_id=2 AND A.c1 BETWEEN '10' AND '30'";
$inner_querry = $this->A->find("all", compact("fields", "conditions"));
$fields = "p1,p2";
$conditions = "p1 IN ($inner_querry) AND p2 IN($inner_query) AND p2 > 100";
$order = "p1";
$query = $this->A->find("all", compact("fields", "conditions", "order"));
debug($query); //check results of for error.
I think that sometimes it's not so wrong to use Model->query(), but let' try to use cake functions.
In fact the only way I see to obtain that particular query is building the subqueries with buildStatement() function (and at the end you still have to call Model->query() so...). But just for fun.
Assuming your model name is MyTable, in your MyTablesController do:
$p1 = "IFNULL(a.c2, '10') AS p1";
$db = $this->MyTable->getDataSource();
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'C',
'fields' => array('MAX(c.c1)'),
'conditions' => array(
"C.c1 > " => 'p1',
)
),
$this->MyTable
);
$p2 = "IFNULL(".$subQuery.") AS p2";
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'A',
'fields' => array($p1, $p2),
'conditions' => array(
"A.user_id" => 2,
"A.c1 BETWEEN ? AND ?" => array(10,30)
)
),
$this->MyTable
);
$query = $db->buildStatement(
array(
'table' => $subQuery,
'alias' => 'test',
'fields' => array('p1', 'p2'),
'conditions' => array(
"p2 > " => 100,
)
),
$this->MyTable
);
Does $this->db->insert_batch(); insert with 1 table connection or does it insert each row separately incurring overhead of opening connections?
From the documentation of code igniter insert_batch do this kind of things
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
// Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
So it would produce only one query with all the values, normally this way faster then doing separate inserts.
To answer your question: It uses one connection.
Actually #RageZ answer based on document is not always correct. Because it's totally based on the number of items you want to insert. When looking at codeigniter insert_batch() code, you can see that they slice batch inserts into 100 items.
// Batch this baby (Around line number 1077 in codeigniter 2.x)
for ($i = 0, $total = count($this->ar_set); $i < $total; $i = $i + 100)
{
$sql = $this->_insert_batch($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_keys, array_slice($this->ar_set, $i, 100));
//echo $sql;
$this->query($sql);
}
It means that your values will be slice to 100s inserts and if you uncomment the echo $sql part you can see what it's look like when you use insert batch for 101 items. So based on your connection preferences there can be more than one connection needed for inserting in db.