首页 > 解决方案 > 我的功能没有运行,我错过了什么?

问题描述

我正在为我的网站制作朋友列表,我需要多次从数据库中获取用户的朋友,所以我认为我需要一个功能来整理东西。我的代码正确显示了我的所有信息,直到我将它放入一个函数然后它停止工作。我没有收到任何错误消息,内容只是消失了。我在其他 php 文档中的其他函数工作正常,所以我不知道问题是什么。

我在 1 个文件夹中有 4 个关键的 php 文档。Friends.php header.php 包含/dbh.inc.php 和包含/friends.inc.php

头文件.php

<?php  
    require 'includes/dbh.inc.php';
    session_start();
    date_default_timezone_set('America/Chicago');
    include 'includes/posts.inc.php';
    include 'includes/friends.inc.php';
?>

包括/dbh.php

<?php

$servername = "na";
$dBUsername = "na";
$dBPassword = "na";
$dBName = "na";

$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);

if (!$conn) {
    die("Connection failed: ".mysqli_connect_error());
}

朋友.php

echo "<div class='friendStatus1'>";
                            echo "Following";
                            status1($conn);
echo "</div>";

最后包括/friends.inc.php

<?php
// status1
    function status1($conn) {
        $stat1sql = "SELECT * FROM friends WHERE (user1 = ?) AND status = 1";
        $stat1stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stat1stmt, $stat1sql)) {
            echo "error1";
        }
        else {
            mysqli_stmt_bind_param($stat1stmt, "i", $userID);
            mysqli_stmt_execute($stat1stmt);
            $stat1result = mysqli_stmt_get_result($stat1stmt);
            while ($stat1row = mysqli_fetch_assoc($stat1result)) {
                $userID == $stat1row['user1'];
                $otheruserID = $stat1row['user2'];
                $otheruserNamesql = "SELECT userName FROM users WHERE userid = ?";
                $otheruserNamestmt = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($otheruserNamestmt, $otheruserNamesql)) {
                    echo "error";
                }
                else {
                    mysqli_stmt_bind_param($otheruserNamestmt, "s", $otheruserID);
                    mysqli_stmt_execute($otheruserNamestmt);
                    $otheruserNameresult = mysqli_stmt_get_result($otheruserNamestmt);
                    while ($otheruserNamerow = mysqli_fetch_assoc($otheruserNameresult)) {
                        echo "<div class='friendBox'>";
                            echo "<a href='home.php?user=".$otheruserNamerow['userName']."'>".$otheruserNamerow['userName']."</a>";
                        echo "</div>";
                    }
                }
            }
        }
    }

?>

标签: php

解决方案


推荐阅读