Mysqli查询不能工作两次

时间:2013-02-02 00:39:58

标签: php database mysqli

我不能让我的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();但没有运气。也许我没有正确使用它?我浏览了很多问题,发现了类似的问题,但是我试过它们都无济于事。我是编程的新手,所以我可能会忽略一些基本的东西。谢谢大家的帮助!

2 个答案:

答案 0 :(得分:5)

你做错了。
从不将您的数据操作代码与演示文稿代码混合在一起。

首先制作一个获取帖子的功能:

function get_posts() {
    global $mysqli;
    $sql = "SELECT variable_name, post_name, post_date, post_display 
            FROM blog_posts ORDER BY id DESC LIMIT 5"
    $result = mysqli_query($mysqli, $sql);
    if (!$result) trigger_error(mysqli_error()."[$sql]");
    $date = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
}

这样称呼

require_once 'database.inc.php';
$mysqli = mysqli_connect($host, $username, $password, $database);
$data = get_posts();

然后使用此数组显示

答案 1 :(得分:-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);

}