简体   繁体   中英

mysqli_query and mysqli_fetch_assoc giving errors

I have been attempting to find a workaround for the mysql error messages found when I access my index.php file, code below:

Warning: mysqli_query() expects parameter 1 to be mysqli, resource given in /Applications/XAMPP/xamppfiles/htdocs/cms/includes/navigation.php on line 24
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given in /Applications/XAMPP/xamppfiles/htdocs/cms/includes/navigation.php on line 26

For the life of me I cannot figure out what I'm doing wrong here as it works when I use static links but when testing dynamic links it just goes awol one.

Below is the code I'm using for the navigation.php file which is running out of the includes folder:

   <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
    <div class="container">

        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Start Bootstrap</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">

            <?php

                $query = "SELECT * FROM categories";
                $please_work = mysqli_query($connection, $query);

                while($row = mysqli_fetch_assoc($please_work)){
                    $cat_title = $row['cat_title'];

                    echo "<li><a href='#'>{$cat_title}</a></li>";

                }

                ?>      

            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

And of course my db.php files which is also located in my includes folder:

<?php

$db['db_host'] = "localhost";
$db['db_user'] = "root";
$db['db_pass'] = "";
$db['db_name'] = "cms";

foreach($db as $key => $value){   
define(strtoupper($key), $value);
}

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

if($connection){

    echo "We are connected";   
}

?>

There's not much need for me to post my index.php as I know for sure the error isn't coming from there as the static content is working when used, meaning the include function is working.

Please can somebody save me from a long headache?

Thanks,

CB

You are mixing mysql_* and mysqli_* functions. You should always use mysqli_* .

So, you need to change this:

$connection = mysql_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

to this:

$connection = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

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