我正在编写一个自定义插件,并使用wordpress页面上的短代码调用插件函数。
global $db_handle;
下面声明了类“DBController”全局的句柄。
if (isset($db_handle))
始终返回False。无法找出原因。我还必须澄清,我首先调用“[连接\\u数据库]”,然后调用“[加载\\u下拉内容=”客户“]”任何帮助都将不胜感激!完整代码如下:
global $db_handle;
function vc_connect_to_database()
{
require_once("dbcontroller.php"); # Instantiate DB COntrolling functions
$db_handle = new DBController();
$connection = $db_handle->connectDB();
if (!$connection)
{
return \'Unable to connect to database\';
}
else
{
return \'<script>alert("Connected to the database successfully.")</script>\';
# return \'<h6>"Database connect successful. This is where the customers list will appear"</h6>\';
}
}
function vc_load_dropdown($atts, $content=null)
{
$atts = shortcode_atts( array(\'content\' => \'customer\'), $atts );
if ($atts[ \'content\' ] == \'customer\')
{
$query = \'SELECT CustomerID, FirstName, LastName FROM customers ORDER BY FirstName, LastName\';
if (isset($db_handle))
{
return \'DB Handle is available\';
}
else
{
return \'DB Handle is NOT available\';
}
# $results = $db_handle->runQuery($query);
}
}
add_shortcode(\'load_dropdown\', \'vc_load_dropdown\');
add_shortcode(\'connect_database\', \'vc_connect_to_database\');