2013-10-04 46 views
2
<?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> 

致命錯誤:無法在C:\ wamp \ www \ forex \ includes \ footer.html中的寫入上下文中使用方法返回值在線3PHP CLASS - 致命錯誤:無法在寫入上下文中使用方法返回值

請幫我解決這個問題..我搜索了很多,並沒有找到任何解決方案。

+2

可能重複不能使用其實現方法具d返回值在寫入上下文](http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context) – Pupil

+0

請參閱這裏:http://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context – Pupil

回答

16

的問題是在頁腳行3:

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

從PHP手冊:

Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.

你必須設置obj- $>地址()到它自己的變量,並用empty()測試這個變量。

+0

是的,這是問題..謝謝! – user2845577

0

函數地址()不返回任何東西,所以你不能使用空($ obj->地址())

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());?> 
+0

如果一個函數返回「Nothing」,它將返回NULL,所以它對'empty()'是絕對有效和完美的。但他的PHP版本很低,所以它不會工作。 – Fleshgrinder

+0

@Fleshgrinder - 你怎麼能說我的PHP版本低?礦是5.4.3,我應該升級嗎? – user2845577

+0

正確,如果您在函數地址()中使用「echo」而不是「return」。 「空」功能不適用於5.4.3,您必須具有5.5或更高版本。 – zerokavn

0

見下文:

<?php if(trim($obj->address()) != ''){?><?php $obj->address();?> 
的[
+0

是的謝謝!這是問題。 – user2845577

相關問題