2

我无法让我的 Mysqli 查询同时工作。如果我在我的 html 中注释掉一个函数,则另一个函数会正确执行,反之亦然。

function all_posts() {
    require_once 'database.inc.php';
    $mysqli = mysqli_connect($host, $username, $password, $database);
    $query = mysqli_query($mysqli, "SELECT variable_name, post_name, post_date, post_display FROM blog_posts ORDER BY id DESC LIMIT 5");

    if (!$query)
        echo mysqli_error();

    while ($results = mysqli_fetch_assoc($query)) {

        $post_name = $results['post_name'];
        $post_date = $results['post_date'];
        $post_display = $results['post_display'];
        $variable_name = $results['variable_name'];

        echo "<a href='posts.php?post={$variable_name}'>";
        echo "<div class='entry'>";
        echo "<div class='entry_header'>";
        echo "<h2>{$post_name}</h2>";
        echo "<h3>{$post_date}</h3>";
        echo "</div>";
        echo "<p>{$post_display}</p>";
        echo "</div>";
        echo "</a>";
    }

    mysqli_free_result();
}

function all_sidebar_posts() {

    require_once 'database.inc.php';
    $mysqli = mysqli_connect($host, $username, $password, $database);
    $query = mysqli_query($mysqli, "SELECT variable_name, post_name FROM blog_posts ORDER BY id DESC LIMIT 5");

    while ($results = mysqli_fetch_assoc($query)) {

        $post_name = $results['post_name'];
        $variable_name = $results['variable_name'];
        echo "<li><a href='posts.php?post=$variable_name'>$post_name</a></li>";
    }

    mysqli_free_result();
}

这是我要输出到的 html。

<ul>
    <?php all_sidebar_posts(); ?>
</ul>
</div>
<div class="content_container">
    <?php all_posts(); ?>
</div>

我试过使用mysqli_data_seek();但没有运气。也许我没有正确使用它?我浏览了很多问题并找到了类似的问题,但我都试过了,但都无济于事。我是编程新手,所以我可能会忽略一些基本的东西。谢谢大家的帮助!

4

2 回答 2

5

你做错了。
切勿将您的数据操作代码与演示代码混合。

首先,将帖子放入数组:

require_once 'database.inc.php';
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $username, $password, $database);

$sql = "SELECT variable_name, post_name, post_date, post_display 
        FROM blog_posts ORDER BY id DESC LIMIT 5"
$result = mysqli_query($mysqli, $sql);
$data = array();
while ($row = mysqli_fetch_assoc($result)) {
    $data[] = $row;
}

然后使用此$data数组在您需要的任何时候显示帖子,只需使用foreach()

于 2013-02-02T06:35:03.497 回答
-1

http://www.php.net/manual/en/mysqli-result.data-seek.php

查阅手册了解 data_seek() 的用法;

举个例子:

$Query = "SELECT * FROM Users WHERE ID='1'";
$TheQuery -> $MySQLi->query($Query);
$Results = $TheQuery->fetch_array(MYSQLI_ASSOC);
$TheQuery->data_seek(0); // Lets you re-use the query

$Count = $TheQuery->num_rows; // Gets the count 

所以在你的情况下:

您应该执行过程方法:

$query = "SELECT Name, CountryCode FROM City ORDER BY Name";

    if ($result = mysqli_query($link, $query)) {

        /* fetch row */
        $row = mysqli_fetch_row($result);

        printf ("City: %s  Countrycode: %s\n", $row[0], $row[1]);


    mysqli_data_seek($result, 0);

    $row_cnt = mysqli_num_rows($result);


        /* free result set*/
        mysqli_free_result($result);

}
于 2013-02-02T01:41:45.520 回答