在Wordpress模板中使用$ wpdb时获取空白页

时间:2012-10-16 21:01:56

标签: php mysql wordpress wordpress-plugin wpdb

我正在尝试使用自定义php代码创建一个Wordpress模板,该代码连接Wordpress数据库中的自定义表,但是它显示了此代码的空白页

<?php
/*
Template Name: JNTU
*/
?>

<?php
function doSomething() {
global $wpdb;
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
$id = $_POST['ID'];

 $add = $wpdb->prepare("SELECT SUM(tech) AS tech4 FROM (
(SELECT SUM(tm) AS tech FROM jbit WHERE id = :id)
UNION ALL
(SELECT SUM(tm) AS tech FROM hmm WHERE id = :id))t1");
$add -> execute(array(':id'=>$id));
echo " <center><table id='mytable' cellspacing='0'  border=3 align=center> 
<tr><TH scope='col'>Total Marks</TH> </tr><center>"; 
while ($row1 = $add->fetch(PDO::FETCH_ASSOC))
{
 echo "<tr>"; 
 echo "<td align=center>" . $row1['tech4']. "</td>";
 echo "</tr>";
  }
}
}
?>

任何想法???

1 个答案:

答案 0 :(得分:0)

这不是$wpdb->prepare的工作原理。 $wpdb->prepare接受一个字符串并返回一个字符串。并且$wpdb类中没有执行方法。 Check the Codex.你想要的是:

$wpdb->get_results(
    $wpdb->prepare("SELECT SUM(tech) AS tech4 FROM (
      (SELECT SUM(tm) AS tech FROM jbit WHERE id = %d)
      UNION ALL
      (SELECT SUM(tm) AS tech FROM hmm WHERE id = %d)
    )",$id,$id)
);

基本上它是printf syntax,但没有'值编号/值交换'部分 - 即这类事物 - %2$s$wpdb->prepare因此很有用但不是很好。