相同的陈述,第二个不同的if

时间:2014-10-05 17:19:09

标签: php mysqli prepared-statement

我试图弄清楚为什么mysqli parepared声明不起作用。我可以说页面有两个部分,第一个需要$ _GET为空,最后一个需要$ _GET [' id']和其他设置(isset)。两个"部分"具有相同的查询,但在最后一节中,首先是不同的查询。

Everyting工作正常,直到最后一个"部分"中的重复查询,没有打印任何内容。

我有这样的事情:

if (login_check($mysqli) == true) {

    if(empty($_GET)) { //first section

        if ($stmt = $mysqli->prepare("SELECT friends.*, members.*, account_type.* FROM friends  INNER JOIN members ON members.id = friends.friendID
                                                                                                INNER JOIN account_type ON account_type.name = members.acc_type
                                                                                                WHERE friends.userID = ? AND members.acc_type = ?")) {

            $stmt->bind_param('is', $_SESSION['user_id'], $_SESSION['acc_type']);
            $stmt->execute();

        } else echo $mysqli->error;

        $result = $stmt->get_result();

        // php/mysqli stuff working as expected ($row[] printing db data)
        // no need to close
    }

    if(isset($_GET['id'], $_SESSION['user_id'])) { // last section

        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
            exit();
        }

        /* prepare statement */
        if ($stmt = $mysqli->prepare("SELECT COUNT(*) rowCount FROM friends WHERE friendID = ? AND userID = ?")) {
            $stmt->bind_param('ii', $_GET['id'], $_SESSION['user_id']);
            $stmt->execute();
            /* bind variables to prepared statement */
            $stmt->bind_result($rowCount);

            /* fetch values */
            if($stmt->fetch()) {
                if ($rowCount > 0) { // This check is working fine, I tested.
                    $stmt->close();  // close here so no "non-object" error
                    $stmt = $mysqli->prepare("SELECT friends.*, members.*, account_type.* FROM friends  INNER JOIN members ON members.id = friends.friendID 
                                                                                                        INNER JOIN account_type ON account_type.name = members.acc_type
                                                                                                        WHERE friends.friendID = ? AND members.acc_type = ?");
                    $stmt->bind_param('is', $_GET['id'], $_SESSION['acc_type']);
                    $stmt->execute();

                    $result = $stmt->get_result();
                    $row = $result->fetch_array();

                    // Here I have some $row['columns'] and nothing is printed.
                } else{
                    echo $_SESSION['username'], ', you are not allowed to be here.';
                }
            }
            /* close statement */
            $stmt->close();
        }
        /* close connection */
        $mysqli->close();
    }
} else {
        echo 'Please, <a href="login">log in</a>.';
}

第一个查询工作正常,另一个是复制/粘贴。

我不知道问题出在哪里:(

谢谢!

编辑:抱歉,第二个查询应该是&#34; WHERE friends.friendID&#34;。

for ($i = 1; $i < 13; $i++) {
    $month = $row['month' . $i];
    if($row[$month] == 1) {
        $paid[] = 'Paid';
    } else {
        $paid[] = 'Not Paid';
    }
    $bonus = $row['bonus'];
}

问题是members.monthx和friends.monthx具有相同的名称,但它们并不意味着具有相同的值,因此我不知道从哪个表获得$ row [&#39; month&# 39; 。 $ i]。该值应取自friends.monthx。我该如何指定?

0 个答案:

没有答案