简体   繁体   中英

PHP MySQL UNION SELECT not working the way expected

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>';
}
?>

Than's :)

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM