3
<?php /* my function class */
class dbFunctions{
public  $dbHost  = "localhost", 
        $dbLogin = "root",
        $dbPwd   = "",
        $dbName  = "forex",
        $mysqli;

/* Create a new mysqli object with database connection parameters */
public function __construct(){
    $this->mysqli = new mysqli($this->dbHost, $this->dbLogin, $this->dbPwd , $this->dbName);
    if(mysqli_connect_errno()) {
        echo "Connection Failed: " . mysqli_connect_errno();
        exit();
    }
}
public function address(){
    if($stmt = $this->mysqli -> prepare("SELECT `email_content` FROM `content` WHERE `content_name`= ? ")) {
        $content = 'address';
        $stmt -> bind_param("s", $content);
        $stmt -> execute();
        $stmt -> bind_result($result);
        $stmt -> fetch();
        $stmt -> close();
        echo $result;
    }
}
}
$obj = new dbFunctions() ;
?>

<!-- MY FOOTER FILE WHERE I CALLED THE FUNCTION-->
     <div id="address">
        <a href="#" style="color:#fff; text-decoration:none;"> Contact Us:</a>
        <?php if(!empty($obj->address())){?><?php $obj->address();?>
        <?php }?>
    </div>

致命错误:无法在第 3 行的 C:\wamp\www\forex\includes\footer.html 的写入上下文中使用方法返回值

请帮我解决这个问题..我搜索了很多,但没有找到任何解决方案。

4

3 回答 3

18

问题出在第 3 行的页脚中:

<?php if(!empty($obj->address())){?><?php $obj->address();?>

来自 PHP 手册:

在 PHP 5.5 之前,empty() 只支持变量;其他任何事情都会导致解析错误。换句话说,以下内容将不起作用:empty(trim($name))。相反,使用 trim($name) == false。

您必须将 $obj->address() 设置为它自己的变量,并使用 empty() 测试该变量。

于 2013-10-04T07:35:22.560 回答
0

函数 address()不返回任何内容,因此您不能使用empty($obj->address())

public function address(){
    if($stmt = $this->mysqli -> prepare("SELECT `email_content` FROM `content` WHERE `content_name`= ? ")) {
        $content = 'address';
        $stmt -> bind_param("s", $content);
        $stmt -> execute();
        $stmt -> bind_result($result);
        $stmt -> fetch();
        $stmt -> close();
        return $result;
    }
    return false;
}

和显示:

<?php if(!empty($obj->address())){?><?php print_r($obj->address());?>
于 2013-10-04T07:37:39.863 回答
0

见下文:

<?php if(trim($obj->address()) != ''){?><?php $obj->address();?>
于 2013-10-04T07:38:50.970 回答