查询的嵌套不起作用

时间:2016-04-18 09:51:32

标签: php mysql

这是php的新手,我编写了一个代码,我嵌套了两个查询,即一个查询在另一个内,但问题是它显示我意外'}'关闭第二个查询的大括号错误,请帮助我。

PHP

<?php
    $con = mysqli_connect("localhost","**","**","**");
    $query = ("SELECT * FROM status");
    $result = mysqli_query($con, $query);
        while ($row = $result->fetch_assoc()) 
            {
                echo '

                <div class="col-lg-12">

                    <div class="social-feed-box">' . 

                    $query = ("SELECT via FROM status WHERE IF(via='" . $_SESSION['via'] . "' ");
                        $result = mysqli_query($con, $query);
                        while ($row = $result->fetch_assoc()) 
                        {
                        '<div class="pull-right social-action dropdown">
                            <button data-toggle="dropdown" class="dropdown-toggle btn-white">
                                <i class="fa fa-angle-down"></i>
                            </button>
                            <ul class="dropdown-menu m-t-xs">
                                <li><a href="post_delete.php">Delete</a></li>
                            </ul>
                        </div>' .}.
                        '<div class="social-avatar">
                            <a href="" class="pull-left">' . 

                            '</a>
                            <div class="media-body">
                                <a href="#">' 
                                    .$row['via'].
                                '</a>' . 
                                '<small class="text-muted">' .$row['created_at']. '</small>
                            </div>
                        </div>
                        <div class="social-body">' . 
                           '<p>' .$row['status']. '</p>

                            <div class="btn-group">
                                <button class="btn btn-white btn-xs"><i class="fa fa-thumbs-up"></i> Like this!</button>


                            </div>
                        </div>
                        <div class="social-footer">


                            <div class="social-comment">
                                <a href="" class="pull-left">' . 
                                    '<img alt="image" src="'.$_SESSION['doc'].'">' . 
                                '</a>

                                <div class="media-body">
                                <div class="form-group">
                                    <textarea class="form-control" placeholder="Write comment..."></textarea>
                                    </div>' . 
                                    '<div class="col-sm-1 form-group"><input type="submit" class="btn btn-primary btn-block" value="Post" align="left"></div>
                                </div>' . 
                                    '<div class="chat-message right">
                                        <div class="message">' . 
                                            '<a class="message-author" href="#">'  .$_SESSION['via']. '</a>' . 
                                            '<span class="message-date">' . '</span>
                                            <span class="message-content">
                                            Hello how are you?
                                            </span>
                                        </div>
                                    </div>

                            </div>

                        </div>

                    </div>

                </div>';
                }
?>

3 个答案:

答案 0 :(得分:0)

我建议您使用任何IDE,如eclipse或sublime 还有两点需要牢记......

  1. 通过使用echo打印html标签,在PHP中执行所有操作。

    <?php echo "<p>this p tag in php style</p>"; ?>

  2. HTML和PHP标记的混合

    <p> <?php echo "this p tag is in partial PHP style"; ?> </p>;

  3. 我希望这会有所帮助......

答案 1 :(得分:0)

嵌套查询位于echo内。尝试结束echo并在第二次查询后开始一个新的echo。可能很聪明地为$ row使用不同的变量,但这不重要。

<?php

$con = mysqli_connect("localhost", "**", "**", "**");
$query = ("SELECT * FROM status");
$result = mysqli_query($con, $query);
while ($row = $result->fetch_assoc()) {
echo '

            <div class="col-lg-12">

                <div class="social-feed-box">';


$query = ("SELECT via FROM status WHERE IF(via='" . $_SESSION['via'] . "') ");
$result = mysqli_query($con, $query);
while ($row = $result->fetch_assoc()) {
    echo '<div class="pull-right social-action dropdown">
                        <button data-toggle="dropdown" class="dropdown-toggle btn-white">
                            <i class="fa fa-angle-down"></i>
                        </button>
                        <ul class="dropdown-menu m-t-xs">
                            <li><a href="post_delete.php">Delete</a></li>
                        </ul>
                    </div>';
}
echo '<div class="social-avatar">
                        <a href="" class="pull-left"></a>
                        <div class="media-body">
                            <a href="#">'
. $row['via'] .
'</a>' .
'<small class="text-muted">' . $row['created_at'] . '</small>
                        </div>
                    </div>
                    <div class="social-body">' .
'<p>' . $row['status'] . '</p>

                        <div class="btn-group">
                            <button class="btn btn-white btn-xs"><i class="fa fa-thumbs-up"></i> Like this!</button>


                        </div>
                    </div>
                    <div class="social-footer">


                        <div class="social-comment">
                            <a href="" class="pull-left">' .
'<img alt="image" src="' . $_SESSION['doc'] . '">' .
'</a>

                            <div class="media-body">
                            <div class="form-group">
                                <textarea class="form-control" placeholder="Write comment..."></textarea>
                                </div>' .
'<div class="col-sm-1 form-group"><input type="submit" class="btn btn-primary btn-block" value="Post" align="left"></div>
                            </div>' .
'<div class="chat-message right">
                                    <div class="message">' .
'<a class="message-author" href="#">' . $_SESSION['via'] . '</a>' .
'<span class="message-date">' . '</span>
                                        <span class="message-content">
                                        Hello how are you?
                                        </span>
                                    </div>
                                </div>

                        </div>

                    </div>

                </div>

            </div>';
}
?>

答案 2 :(得分:0)

我认为@LordMarty正走在正确的道路上。您可以在&#34; echo&#34;中编写PHP。但它会忽略它并尝试将其打印出来。尝试将PHP(和您的SQL查询)保留在任何回声和HTML标记之外。

Maby尝试关闭PHP,继续使用HTML,然后重新打开PHP。

<?php
$con = mysqli_connect("localhost","**","**","**");
$query = ("SELECT * FROM status");
$result = mysqli_query($con, $query);
    while ($row = $result->fetch_assoc()) 
        {
            ?>

            <div class="col-lg-12">

                <div class="social-feed-box">' . 
            <?php
                $query = ("SELECT via FROM status WHERE IF(via='" . $_SESSION['via'] . "' ");
                    $result = mysqli_query($con, $query);
                    while ($row = $result->fetch_assoc())