wordpress wpdb类确实很强大,但在实际运用中很容易出错,一定注意: 如果想查询数据库返回的行数,官方文档给的案例 $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;")); echo '<p>User count is ' . $user_count . '</p>'; 运行后你会发现报错:Warning: Missing argument 2 for wpdb::prepare(), called in E:\ComsenzEXP\wwwroot\90xyy\wp-content\themes\90xyy\page-xuexiao.php on line 24 and defined inE:\ComsenzEXP\wwwroot\90xyy\wp-includes\wp-db.php on line 1246 百度找不到答案,google一下才知道原来是少了参数,改一下代码即可正常输出: $user_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->users;",'')); echo '<p>User count is ' . $user_count . '</p>'; (责任编辑:最模板) |