很新的使用MySQL,但是,我試圖修復一塊舊的代碼中的bug的WordPress插件 - 這裏是原代碼:WordPress的mysql_fetch_array()預計參數1是資源錯誤
$sql = mysqli_query("SELECT count(`question_count`) as Qcount FROM `wp_posts` WHERE `question_count` = 1 and `question_date` = '".date("Y-m-d")."'") or die(mysql_error());
$no_of_questions = get_option('askme_setting_no_of_questions', 10);
if($row = mysql_fetch_array($sql)) {
$qry = $row['Qcount'];
}
if($qry >= $no_of_questions) {
$value = "The question limit for today has been reached";
$button = "disabled";
} else {
$value = "Add your question to the cart";
$button = " ";
}
這是給下面的錯誤:
mysqli_query() expects at least 2 parameters, 1 given in
改以來的第一行我有如下使用WordPress的功能:
$sql = $wpdb->get_results("SELECT count(`question_count`) as Qcount FROM `wp_posts` WHERE `question_count` = 1 and `question_date` = '".date("Y-m-d")."'");
現在這給了以下錯誤:
mysql_fetch_array() expects parameter 1 to be resource, array given in ...
Undefined variable: qry in ...
有什麼明顯的,我做錯了什麼?
不要使用'mysqli'。獨家使用[WordPress數據庫層WPDB](https://codex.wordpress.org/Class_Reference/wpdb),不要混用。這裏真正的問題是你試圖使用'mysqli',而不是無意中使用糟糕的舊'mysql_query'界面。 – tadman
@tadman完全同意你的觀點 - 我的目標是用$ wpdb替換對mysqli和mysql_query接口的引用(正如你從第一次更改中看到的那樣) - 但是,我不知道如何改變我的第二部分代碼適合第一個 - 如果這是有道理的 –
你不能使用'mysql_fetch_array'或類似的東西。忘記這些功能,它們不相關,不兼容。 *獨佔*到WPDB功能。在文檔中,他們引用'get_row'來表示結果,等等。使用這些。 – tadman