PHP MySQL UNION SELECT无法按预期方式工作 [英] PHP MySQL UNION SELECT not working the way expected
问题描述
这是问题,如果仅执行第一个选择,它就可以完美运行,但是当我执行UNION时,它回显警告:mysqli_fetch_assoc()期望参数1为mysqli_result,布尔值在C:\ bla,bla,bla中给出.
here's the thing, if I do just the first select it works perfect, but when I do the UNION it echoes Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\bla, bla, bla.
我搜索了类似的问题和/或问题,但是没有发现似乎为我提供了解决此问题的线索.
I did a searched for similar question and/or problem but none of the found seemed to give me a clue of how to fix this.
<?php
$moncols='col1,col2,col3,col4';
$main = "SELECT $moncols FROM table1 WHERE col4 = 'yes'
UNION
SELECT $moncols FROM table2 WHERE col4 = 'yes'
UNION
SELECT $moncols FROM table3 WHERE col4 = 'yes'
UNION
SELECT $moncols FROM table4 WHERE col4 = 'yes'
UNION
SELECT $moncols FROM table5 WHERE col4 = 'yes'";
$resoult = $mon -> query($main);
while ($row = mysqli_fetch_assoc($resoult)){
$myStuff=$row['col4'];
$otherStuff=$row['col3'];
echo $myStuff. ' - ' .$otherStuff. '<br>';
}
?>
比的:)
推荐答案
您的查询中可能有一个错误.也许您的表之一缺少这些列之一.无论哪种情况,$resoult
都不是有效的mysqli结果.尝试添加一些错误检查以查找问题:
You likely have an error in your query. Perhaps one of your tables is missing one of those columns. Whatever the case, $resoult
is not a valid mysqli result. Try adding some error checking to find the problem:
$resoult = $mon->query($main);
if (! resoult) {
echo "MySQL error: " . $mon->error);
}
这篇关于PHP MySQL UNION SELECT无法按预期方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!