我已经为我的Wordpress站点编写了一些基本的定制PHP代码。代码基本上访问MySQL数据库(与wordpress数据库不同的数据库)并显示信息。除了在主页上运行它之外,我还需要在侧边栏上运行它。代码在主页或侧栏中运行良好,但当我将代码同时放在主页和侧栏中时,侧栏中出现以下错误:
Fatal error: Call to a member function prepare() on a non-object in /home/kimusubi/public_html/wp-content/plugins/php-code-widget/execphp.php(44) : eval()\'d code on line 5
我认为这是一个同时访问同一个表和数据库的问题,但我正在使用include\\u一次,并在使用完后立即关闭连接,因此我不确定问题出在哪里。这是我用于侧栏和主列页面的代码:
<?php
include_once \'db_config.php\'; //Include database configuration file
$current_user = wp_get_current_user()->user_login; //Get current WP logged in user
try { //Access accounts table and pull user info into associative array
$STH = $DBH->prepare("SELECT * FROM accounts WHERE accnt = :accnt");
$STH->bindParam(\':accnt\', $current_user);
$STH->execute();
$Result = $STH->fetch(PDO::FETCH_ASSOC);
$DBH = null;
}
catch(PDOException $e) {
echo "Select Accounts Table Error: " .$e->getMessage(). "</br>"; //Display Error (if any)
}
$available = min($Result[\'week_two\'],$Result[\'week_one\'],$Result[\'current\']);
echo "Account: " .$Result[\'accnt\']. "</br>";
echo "Name: " .$Result[\'first\']. " " .$Result[\'last\']. "</br>";
echo "Address: " .$Result[\'address\']. ", " .$Result[\'city\']. ", " .$Result[\'state\']. ", " .$Result[\'zip\']. "</br>";
echo "Phone: " .$Result[\'phone\']. "</br>";
echo "Current Balance: $" .$Result[\'current\']. "</br>";
echo "Available Balance: $" .$available. "</br>";
?>
这是我的db\\U配置中的内容。php文件:
<?php
try {
$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
}
catch(PDOException $e) {
echo "Select DB Error: " .$e->getMessage(). "</br>";
}
?>
Exec PHP是我使用的唯一一个直接从Wordpress执行PHP的插件。如果有任何指导,我将不胜感激。