将MySQL转换为WordPress$WPDB

时间:2018-08-14 作者:chef_3072485

我正在从3.7升级一个古老的Wordpress网站。它有一些SQL查询请求,在3.9以上版本之后无法运行,您现在必须通过$WPDB连接到DB(https://codex.wordpress.org/Class_Reference/wpdb). 我对此进行了初步尝试,但挂断了mysql\\u fetch\\u array部分的连接,也不清楚我所做的是否正确。mysql\\u fetch\\u数组似乎需要使用get\\u结果(https://developer.wordpress.org/reference/classes/wpdb/get_results/).

我完全意识到,这不是一种理想的方式来实现这一点,并完全打算使一切现代化,但长话短说,如果可能的话,能够在这种混乱的旧范式中解决这一问题将非常有帮助。

附件是原始代码,然后是我的尝试(还添加了v2尝试)。谢谢你看一看。

SQL:

$sql = "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = \'620\' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no"; 
$result = mysql_query($sql) or die(\'Content was not loaded. Please refresh your page.\'); 
$counter = 0; 
while($post = mysql_fetch_array($result)) { 

    $sqlSecond = "SELECT meta_value FROM wp_postmeta WHERE meta_id = \'".$post[\'value\']."\'"; 
    $result1 = mysql_query($sqlSecond) or die(\'Content was not loaded.\'); 
    while($post1 = mysql_fetch_array($result1)) { 
        $data[$counter] = $post1[\'meta_value\']; 
        $counter++; 
    } 

} 

// get image 
$sql = "SELECT meta_value FROM wp_postmeta WHERE post_id = \'".$data[\'1\']."\' AND meta_key = \'_wp_attached_file\'"; 
$result = mysql_query($sql) or die(\'Content was not loaded.\'); 
$counter = 0; 
while($post = mysql_fetch_array($result)) { 
    $imgURL = $post[\'meta_value\']; 
} 
<小时>

WPDB v1:

global $wpdb;

$sql = $wpdb->prepare( "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = \'620\' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no" );
$result = $wpdb->query ( $sql ) or die(\'Content was not loaded.\');
$counter = 0;
while($post = mysql_fetch_array($result)) {

    $sqlSecond = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post[\'value\'] );
    $result1 = $wpdb->query ( $sqlSecond ) or die(\'Content was not loaded.\');
    while($post1 = mysql_fetch_array($result1)) {
          $data[$counter] = $post1[\'meta_value\'];
          $counter++;
    }

}

// get image
$sql = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE post_id = %s AND meta_key = \'_wp_attached_file\'", $data[\'1\'] );
$result = $wpdb->query ( $sql ) or die(\'Content was not loaded.\');
$counter = 0;
while($post = mysql_fetch_array($result)) {
    $imgURL = $post[\'meta_value\'];
}
<小时>

WPDB v2:

global $wpdb;

$sql = $wpdb->prepare( "SELECT wp_acf_values.value FROM wp_acf_values, wp_acf_fields WHERE wp_acf_fields.post_id = \'620\' AND wp_acf_fields.id = wp_acf_values.field_id ORDER BY wp_acf_fields.order_no" );
$result = $wpdb->get_results ( $sql ) or die(\'Content was not loaded.\');
$counter = 0;
foreach ($result as $post ) {
  $sqlSecond = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE meta_id = %s", $post->value );
  $result1 = $wpdb->get_results ( $sqlTitle ) or die(\'Content was not loaded.\');
  foreach ( $result1 as $post1 ) {
     $data[$counter] = $post1->meta_value;
     $counter++;
  }
}

// get image
$sql = $wpdb->prepare( "SELECT meta_value FROM wp_postmeta WHERE post_id = %s AND meta_key = \'_wp_attached_file\'", $data[\'1\'] );
$result = $wpdb->get_results ( $sql ) or die(\'Content was not loaded.\');
$counter = 0;
foreach ( $result as $post ) {
  $imgURL = $post->meta_value;
  $counter++;
}

1 个回复
SO网友:Tom J Nowell

这里的根本问题是WordPress处理的假设mysql 功能和mysql 对象,了解这些函数很有帮助。

事实是,这本身就是一个完整的API,因此mysql_ 提供的函数在SQL语法之外没有用处或相关性。

例如:

$result = $wpdb->query ( $sql ) or die(\'Content was not loaded.\');
$counter = 0;
while($post = mysql_fetch_array($result)) {
这里的假设是$wpdb->query 返回结果对象,但这不是真的。

如果我们看看query 通过查看文档中的示例,我们可以看到:

此函数返回一个整数值,指示受影响/选定的行数

而且:

如果遇到MySQL错误,函数将返回FALSE。请注意,由于行查询可能同时返回0和FALSE,因此在检查返回值时应小心。

那么如何获取行呢

使用get_results:

$myrows = $wpdb->get_results( "SELECT id, name FROM mytable" );
不要忘记运行SQL语句$wpdb->prepare 首先要防止注射攻击!

还有其他方法允许您获取单数值、行或列,但这里$myrows 字面上是一个选定行的数组(或错误响应)。还有第二个参数get_results 用于确定返回类型,例如关联数组、带键的对象等

https://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results

第二个和第三个查询的问题

即使您修复了这些查询,它们基本上都是错误的做法,因为它们有一个更快、更简单的替换:

$value = get_post_meta( $post_id, \'key\' );
$value 现在包含该帖子的所有meta值和meta键key.

您还可以使用WP_Query 通过其元键和值获取帖子(但请注意,无论您使用的是API还是直接SQL,这都是影响性能的主要因素)

可能有一个ACF函数或API可以在开始时取代直接SQL查询。很少需要执行直接SQL查询。在我的日常工作中,我会查看处理数十亿页面浏览量和数百万行代码的主要网站的代码,其中我只遇到过几次直接的SQL查询

结束