gpt4 book ai didi

php - UNION 2 sql 查询 - wp_posts 与 wp_terms - PHP, WORDPRESS

转载 作者:行者123 更新时间:2023-11-29 07:38:21 25 4
gpt4 key购买 nike

我有一个页面,其中显示所有分类类别和所有帖子/页面。在那里我需要有 ajax 分页,这样我的页面就不会加载年龄(甚至不加载)。因此,我通过 sql 查询构建了分页,现在需要使用 sql 查询获取术语和帖子,然后将它们合并,以便我的分页能够正常工作并计算所有内容。

这是我的代码:

$sql1 = "SELECT * FROM wp_term_taxonomy LEFT JOIN wp_terms ON (wp_term_taxonomy.term_id = 
wp_terms.term_id) WHERE wp_term_taxonomy.taxonomy = 'product-category'";

$sql2 = "SELECT * FROM wp_posts WHERE (post_type = 'products' OR post_type = 'page')
AND (post_status = 'publish' OR post_status = 'suspended') AND ID NOT IN
(231, 236, 1070, 1245) order by post_title ASC";

$sql = $sql1." UNION ".$sql2;

$query = $sql . " limit " . $start . "," . $resultPerPage->perpage;
$list = $db_handle->runBackendQuery($query);

上面返回以下错误:

Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given in .../dbcontroller.php on line 29

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in
.../dbcontroller.php on line 38

我想我可以加入这两种不同的东西。有什么我想念的吗?

最佳答案

错误可能发生是因为您在查询中有一些错误来避免这种情况

您应该选择相同数量的列和相应的数据类型,因此请尽量避免全选 (*) 并使用显式选择列您应该使用如下语法:

$sql1 = "SELECT col_a1, col_a2, col_a3 FROM wp_term_taxonomy LEFT JOIN wp_terms ON (wp_term_taxonomy.term_id = 
wp_terms.term_id) WHERE wp_term_taxonomy.taxonomy = 'product-category'";

$sql2 = "SELECT col_b1, col_b2, col_b3 FROM wp_posts WHERE (post_type = 'products' OR post_type = 'page')
AND (post_status = 'publish' OR post_status = 'suspended') AND ID NOT IN
(231, 236, 1070, 1245) order by post_title ASC";

$sql = $sql1." UNION ".$sql2;

$query = $sql . " limit " . $start . "," . $resultPerPage->perpage;
$list = $db_handle->runBackendQuery($query);

关于php - UNION 2 sql 查询 - wp_posts 与 wp_terms - PHP, WORDPRESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47680419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com