将我的 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
大大收到任何指点!我目前有几页显示草皮:)
谢谢
蒙古
最佳答案
您很可能需要将 WPDB 全局化。
尝试添加
global $wpdb;
到你脚本的顶部?
关于php - 将 MYSQL 转换为 Wordpress $WPDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34010367/