gpt4 book ai didi

php - mysqli_query 和 mysqli_fetch_assoc 给出错误

转载 作者:可可西里 更新时间:2023-11-01 08:40:33 24 4
gpt4 key购买 nike

我一直在尝试为访问我的 index.php 文件时发现的 mysql 错误消息找到解决方法,代码如下:

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

对于我的生活,我无法弄清楚我在这里做错了什么,因为它在我使用静态链接时有效,但在测试动态链接时它只是一个错误。

下面是我用于 navigation.php 文件的代码,该文件正在包含文件夹中运行:

   <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>

当然还有我的 db.php 文件,它也位于我的 includes 文件夹中:

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

?>

我不太需要发布我的 index.php,因为我确信错误不是来自那里,因为静态内容在使用时正在工作,这意味着包含函数正在工作。

谁能帮我摆脱长期头痛的困扰?

谢谢,

CB

最佳答案

您正在混合使用 mysql_*mysqli_* 函数。您应该始终使用 mysqli_*

所以,你需要改变这个:

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

为此:

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

关于php - mysqli_query 和 mysqli_fetch_assoc 给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33798529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com