我試圖爲基於php的論壇製作自定義函數。我將我的索引登陸風格頁面分開到我的phpbb論壇。我試圖顯示最新的註冊用戶用戶名,作爲主頁統計類型小部件的一部分。我有以下代碼至今:如何從結果集中獲取字段並以頁面形式打印爲字符串(php/mysql/phpbb)
//Function code
//Total Posts calculation
$result = $mysqli->query("SELECT DISTINCT post_id FROM phpBB_posts
WHERE post_id <= '1'");
$total_posts = $result->num_rows;
//Total topics calculation
$result2 = $mysqli->query("SELECT DISTINCT topic_id FROM phpBB_topics WHERE topic_id <= '1'");
$total_topics = $result2->num_rows;
//Member count calculation
$result3 = $mysqli->query("SELECT DISTINCT user_id FROM phpBB_users WHERE user_id <= '1'");
$total_members = $result3->num_rows;
//Newest member
$result4 = $mysqli->query("SELECT * FROM phpBB_users WHERE group_id <> 6 ORDER BY user_regdate DESC LIMIT 1");
$newestMember = $result4->name;
//End of function
//function output
printf("Total Posts: ".$total_posts."<br/> Total Topics: ".$total_topics."<br/>Total Members: ".$total_members."<br/>Our newest member is: ".$newestMember);
的「新成員」部分中,我遇到麻煩了,剩下的工作,因爲我想,如果你有任何指針/批評我會很樂意把它上董事會雖然。
我希望能夠從我從該查詢返回的結果集中返回username
列中的值,並將其作爲變量$newestMember
打印出來。
我的邏輯你可以看到的是獲得用戶列表,因此SELECT *
然後將列表限制爲實際用戶並排除機器人等WHERE group_id <> 6
。然後按登記日期ORDER BY user_regdate
對列表進行排序,然後從DESC LIMIT 1
的整個列表中只選擇一行。我一直在檢查mysql和php文檔,並且無法確定如何從返回的行/用戶名列(字段)獲取值並將其作爲newestMember變量存儲並打印到頁面。 任何人都可以指向正確的方向嗎?
乾杯,傑米
您需要_fetch_行結果集。 – CBroe
@CBroe所以使用文檔,如果代替'$ newestMember = $ result4-> name;'我使用以下 'if($ result = $ mysqli-> query($ result4)){ /* fetch對象數組*/ while($ row = $ result-> fetch_row())printf(「%s(%s)\ n」,$ row [0],$ row [1]); } /*免費結果集*/ $ result-> close(); }' 我得到以下錯誤「警告:mysqli :: query()期望參數1是字符串」任何想法? –
這裏的想法是研究錯誤消息... – CBroe