C
C
Chesterfield252021-06-09 23:18:23
PHP
Chesterfield25, 2021-06-09 23:18:23

How to get an array?

Working source code

<?php
                                $con = mysqli_connect("localhost","root","root","demo");

                                $brand_query = "SELECT * FROM xf_category";
                                $brand_query_run  = mysqli_query($con, $brand_query);

                                if(mysqli_num_rows($brand_query_run) > 0)
                                {
                                    foreach($brand_query_run as $brandlist)
                                    {
                                        $checked = [];
                                        if(isset($_GET['category']))
                                        {
                                            $checked = $_GET['category'];
                                        }
                                        ?>
                                            <div>
                                                <input type="checkbox" name="category[]" value="<?= $brandlist['id']; ?>" 
                                                    <?php if(in_array($brandlist['id'], $checked)){ echo "checked"; } ?>
                                                 />
                                                <?= $brandlist['title']; ?>
                                            </div>
                                        <?php
                                    }
                                }
                                else
                                {
                                    echo "No Brands Found";
                                }
                            ?>


Corrected for this code

<?php
                                
                                if(mysqli_num_rows($category_query) > 0)
                                {
                                    foreach($category_query as $categorylist)
                                    {
                                        $checked = [];
                                        if(isset($_GET['category']))
                                        {
                                            $checked = $_GET['category'];
                                        }
                                        ?>
                                            <div>
                                                <input type="checkbox" name="category[]" value="<?= $categorylist['id']; ?>" 
                                                    <?php if(in_array($brandlist['id'], $checked)){ echo "checked"; } ?>
                                                 />
                                                <?= $categorylist['title']; ?>
                                            </div>
                                        <?php
                                    }
                                }
                                else
                                {
                                    echo "No Categorys Found";
                                }
                            ?>


And in function.php

//Read xf_category
$sql = $pdo->prepare("SELECT * FROM xf_category");
$sql->execute();
$category_query = $sql->fetchAll(PDO::FETCH_OBJ);


I get an error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, array given in


60c121e891ae5263797808.png

Tell me what I did wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yuriev, 2021-06-09
@dyuriev

Tell me what I did wrong?

https://www.php.net/manual/ru/mysqli-result.num-ro...
result
Procedural style only: A mysqli_result object obtained with mysqli_query(), mysqli_store_result() or mysqli_use_result() or mysqli_stmt_get_result().

https://www.php.net/manual/en/pdostatement.fetchall.php
PDOStatement::fetchAll — Returns an array containing all rows in the result set

instead of mysqli_num_rows it asks for count() at worst

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question