2012-07-21 68 views
0

這裏的功能:傳遞一個mysql連接到功能

function getCount($module, $db_link) { 
    if($module == "tweets") { 
     $query = "SELECT COUNT(*) FROM tweets WHERE `read`='n'"; 
     $tweet_count = mysqli_query($db_link,$query); 
     echo $tweet_count; 
    } 
} 

這裏的鏈接(這是在代碼前面定義):

$mysqli = mysqli_connect("localhost", "twitterd", "password", "twitterd") or die('Cannot connect to the database'); 

函數調用:

<?php getCount("tweets", $mysqli); ?> 

如何將$mysqli鏈接傳遞給getCount函數?目前,我得到這個錯誤:

Catchable fatal error: Object of class mysqli_result could not be converted to string in lib.php on line 7

+0

這是什麼問題? – 2012-07-21 04:52:24

回答

2

一切都被傳遞得很好;問題是這樣的:

echo $tweet_count; 

您無法將MySQL資源轉換爲字符串。取行:

$row = mysqli_fetch_row($tweet_count); 
echo $row[0];