我有以下代码告诉我是否记录了" widgets_active"在wp_options表的option_name列中存在。
global $wpdb;
$table_name = $wpdb->prefix . "options";
$ActiveWidgets ="widgets_active";
$CheckAllActiveWidgets = $wpdb->get_results("SELECT `option_name` FROM '$table_name' WHERE `option_name` = '$ActiveWidgets'");
echo '<script type="text/javascript">alert("'.$CheckAllActiveWidgets.'");</script>';
if (mysql_num_rows($CheckAllActiveWidgets) == 0) {
然而,当我运行该功能时,警报只是说“阵列”。和错误
显示警告:mysql_num_rows()期望参数1是资源,数组 在......第6行......
,如果我只运行SQL statment mySQL,它会返回我放在那里的1条记录。如何修复上面的代码?
答案 0 :(得分:1)
我并不是WordPress的专家,但根据Wordpress文档(https://codex.wordpress.org/Class_Reference/wpdb),您应该可以使用$wpdb->num_rows
计算查询结果。所以你的代码应该是这样的:
global $wpdb;
$table_name = $wpdb->prefix . "options";
$ActiveWidgets ="widgets_active";
$CheckAllActiveWidgets = $wpdb->get_results("SELECT `option_name` FROM '$table_name' WHERE `option_name` = '$ActiveWidgets'");
echo '<script type="text/javascript">alert("'.$CheckAllActiveWidgets.'");</script>';
if ($wpdb->num_rows == 0) {
答案 1 :(得分:0)
那是因为$CheckAllActiveWidgets
拥有一个数组!
试试这个:
echo '<script type="text/javascript">alert("' . $CheckAllActiveWidgets[0][0] . '");</script>';
`
答案 2 :(得分:0)
我更改了代码以在SQL本身中进行计数。
global $wpdb;
$table_name = $wpdb->prefix . "options";
$CheckAllActiveWidgets = $wpdb->get_var("SELECT COUNT(*) FROM `wp_options` WHERE `option_name` = 'siteorigin_widgets_active'");
if ($CheckAllActiveWidgets == 0) {