将MYSQL转换为Wordpress $ WPDB

时间:2015-12-01 00:06:14

标签: php mysql wordpress

将Wordpress网站升级到4.3.1后,无法转换我的PHP。

原始代码是:

$result = mysql_query("SELECT *,
                        CASE Position 
                          WHEN '1' THEN 'First'
                          WHEN '2' THEN 'Second'
                          WHEN '3' THEN 'Third'
                        END as PositionLong
                      FROM People 
                      WHERE
                         (Position='1' or
                          Position='2' or
                          Position='3') and
                          Season = '2015'
                      ORDER BY Number");
while($row = mysql_fetch_assoc($result)){
  echo '<div class="column-left"><img src="/wp-content/uploads/profile/'.$row['Id'].'.jpg" alt="" />',
       '<h3 class="widget-title"><a href="profiles?title='.$row['Id'].'">',
        $row['FirstName'].' '.$row['Surname'].'</a></h3><br />',
       '<pos>'.$row['PositionLong'].' #'.$row['Number'].'</pos></div>';       
                                       }

所以......我尝试阅读一些指南等,并尝试......替换为:

$result = $wpdb->get_results("SELECT *,
                        CASE Position 
                          WHEN '1' THEN 'First'
                          WHEN '2' THEN 'Second'
                          WHEN '3' THEN 'Third'
                        END as PositionLong
                      FROM People 
                      WHERE
                         (Position='1' or
                          Position='2' or
                          Position='3') and
                          Season = '2015'
                      ORDER BY Number");
foreach ($result as $row){
  echo '<div class="column-left"><img src="/wp-content/uploads/profile/'.$row['Id'].'.jpg" alt="" />',
       '<h3 class="widget-title"><a href="profiles?title='.$row['Id'].'">',
        $row['FirstName'].' '.$row['Surname'].'</a></h3><br />',
       '<pos>'.$row['PositionLong'].' #'.$row['Number'].'</pos></div>';       
                                       }

但是我最终得到了:

Fatal error: Call to a member function get_results() on a non-object

任何指针都很受欢迎!我现在有几页显示草皮:)

谢谢

蒙戈

1 个答案:

答案 0 :(得分:0)

您最有可能需要将WPDB全局化。

尝试添加

global $wpdb;

到你的脚本顶部?