Php mysql, variable help query help
I have this mysql query:
UPDATE `table`.`wp_12_postmeta`
SET `meta_value` = 'yyy'
WHER开发者_Go百科E `wp_12_postmeta`.`meta_id` =5
LIMIT 1 ;
How do i incorporate this:
- instead of
wp_12_
i want a variable$prefix
(variable holds wp_4_, wp_3_, etc) - instead of
yyy
i want a value$perf
(variable is a name ) - instead of
5
i want a value$meta_id
(variable is a nr)
Thank u!
P.S.
here is what i use and it works:
$wpdb->query("UPDATE ".$prefix."postmeta SET meta_value = '".$perf."' WHERE meta_id = '".$meta_id."' LIMIT 1 ");
Problem is, when i execute this query, severl post meta fields are updated, instead of just one. Ty
Use:
$query = sprintf("UPDATE `table`.`%s`
SET `meta_value` = '%s'
WHERE `%s`.`meta_id` = %d
LIMIT 1 ",
mysql_real_escape_string($prefix),
mysql_real_escape_string($perf),
mysql_real_escape_string($prefix),
mysql_real_escape_string($meta_id));
Here's how I would write this with PDO:
$prefix = "wp_4_";
$sql = "UPDATE `table`.`{$prefix}postmeta` SET `meta_value` = ?
WHERE `{$prefix}postmeta`.`meta_id` = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($perf, $meta_id));
精彩评论