0

我正在尝试使用自定义 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>";
  }
}
}
?>

任何想法???

4

1 回答 1

0

这不是如何$wpdb->prepare工作的。$wpdb->prepare接受一个字符串并返回一个字符串。并且类中没有执行方法$wpdb检查法典。你想要的是:

$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语法,但没有“值编号/值交换”部分——即这种类型的东西—— %2$s$wpdb->prepare是有用的,但不是很好,因此。

于 2012-10-17T05:15:17.410 回答