Php mysql,变量帮助查询帮助

时间:2022-09-29 07:37:21

I have this mysql query:

我有这个mysql查询:

UPDATE `table`.`wp_12_postmeta` 
   SET `meta_value` = 'yyy' 
 WHERE `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)
  • 我想要一个变量$前缀而不是wp_12_(变量包含wp_4_, wp_3_等)
  • instead of yyy i want a value $perf (variable is a name )
  • 而不是yyy,我想要一个值$perf(变量是一个名称)
  • instead of 5 i want a value $meta_id (variable is a nr)
  • 我要的不是5,而是$meta_id(变量是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

问题是,当我执行这个查询时,severl post meta字段会被更新,而不是一个。泰

2 个解决方案

#1


1  

Here's how I would write this with PDO:

下面是我如何用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));

#2


2  

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));

#1


1  

Here's how I would write this with PDO:

下面是我如何用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));

#2


2  

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));