Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in

Hello. I have a problem too, please help me.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in

<?php
require_once "conf.php";
?>

<?php

if (isset($_GET['id'])) {
    $id = $_GET['id'];
    $photogall = mysqli_query( "select * from `images` where id='$id'");
} else {
    $photogall = mysqli_query($connect, "select * from images ORDER BY sort");
}
?>
<?php
if (!isset($_POST['id'])) {
    ?>
    <?php
}
?>
<div align="center" class="codrops-demos">
    <div>
        <p align="center" class="title_cat"><b>Galeriaa</b></p>
        <!--<div align="center"><img class="logoimg" src="images/hr1.png "></div>-->
        <div align="center">
            <?php
            if (isset($_GET['id'])) {
                $id = $_GET['id'];
                $gall_row = mysqli_fetch_array($photogall);
                $pho = mysqli_query("select * from photos where gal='$id'");
                echo "<div align=\"center\" class=\"photogall\">" . $gall_row['name'] . "</div>";
                ?>
                <div class="col">
                    <div class="card shadow-sm">
                        <div align="center" class="fotogallist rounded">
                            <?php
                            while ($phorow = mysqli_fetch_array($pho)) {
                                $photo = $phorow['photo'];
                                echo "<div class=\"col\">
                    <div class=\"card shadow-sm\"><img class=\"fotogallist\" src=\"" . $media_url . "user_data/photogall/" . $id . "/" . $photo . "\" alt=\"" . $gall_row['name'] . "\">
</div>
                </div><br>";//BR ultimul
                            }
                            ?>
                        </div>
                    </div>
                </div>
                <!--<div align="center"><img class="logoimg" src="images/hr1.png"></div>-->
                <?php
            } else {
                ?>
                <div class="row row-cols-1 row-cols-sm-2 row-cols-md-4 g-3">
                    <?php
                   
                    while ($gall_row = mysqli_fetch_array($photogall))
                   
                    {
                      ?>  
                        <div class="col">
                        
                            <div class="card shadow-sm">
                                <a
                                        href="index.php?page=photogal2&id=<?php echo $gall_row['id']; ?>"><img
                                            class="autofoto"
                                            src="<?php echo $media_url; ?>user_data/photogall/<?php echo $gall_row['id'] . "/" . $gall_row['logo']; ?> "
                                            alt="<?php echo $gall_row['name']; ?>"
                                            title="<?php echo $gall_row['name']; ?>"></a>
                                <div class="card-body bg-dark">
                                    <p class="card-text"><b><?php echo $gall_row['name']; ?></b></p>
                                    <div class="d-flex justify-content-center align-items-center">
                                        <small class="text-muted">
                                            <script type="text/javascript" src="//yastatic.net/share/share.js"
                                                    charset="utf-8"></script>
                                            <div class="yashare-auto-init" data-yashareL10n="ru"
                                                 data-yashareType="small"
                                                 data-yashareQuickServices="vkontakte,facebook,twitter,odnoklassniki,moimir,gplus"
                                                 data-yashareTheme="counter"></div>
                                        </small>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
</div>

Your query failed. Probably because you forgot something on line 9. (Compare to line 11)

1 Like

What debugging have you done so far? The error message will tell you which line of code your script is failing on, what’s on that line of code?

PHP is not my area of expertise, but should we not be recommending these mysqli_queries be replaced with Prepared Statements?

If I google ‘mysqli_query’, the first results is from php.net and in a big pink box on that page we have the following.

Warning

Security warning: SQL injection

If the query contains any variable input then parameterized prepared statements should be used instead. Alternatively, the data must be properly formatted and all strings must be escaped using the mysqli_real_escape_string() function.

This is a bit of a tangent from the question, but probably worth pointing out.

To be more specific, forget using mysqli_real_escape_string() and do use prepared statements instead.
Using PDO over mysqli makes life much easier too.
I assume the OP is new to PHP. We see too many beginners start off the wrong way, using out-dated methods from the start, it’s a dark path to take.

1 Like

The OP is probably using a tutorial from somewhere that’s well out of date

2 Likes

Yes, that’s usually how this happens. The internet is awash with outdated PHP tutorials.

It’s not usually long before someone mentions prepared statement (and it’s almost instant on sites like SO), but first to get the actual queries running by supplying the required parameters that are shown in the documentation.

To be honest, the whole block of code is a showcase for “how not to do things”. The absence of prepared statements is just one of several things.
Sure we can make it “work” but to make it right it needs to be started over…

2 Likes

hi guys. I solved. thank you very much for the advice.

the whole problem was in the $connect variable.
and it works perfectly

mysqli_query($conn, ‘SELECT * from images WHERE id="’ . $id . '" ')