Mysqli查询无法正常工作两次 [英] Mysqli query doesn't work twice

查看:152
本文介绍了Mysqli查询无法正常工作两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法同时执行Mysqli查询.如果我在html中注释了一个函数,则另一个函数将正确执行,反之亦然.

I cannot get my Mysqli queries to both work. If I comment out one function in my html, the other function is properly executed and vice versa.

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.

Here is the html that I am outputting to.

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

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

I have tried using mysqli_data_seek(); but haven't had luck. Perhaps I am not using it right? I have browsed many questions and found similar ones but I have tried them all to no avail. I am new to programming so I may be overlooking something basic. Thank you all for the help!

推荐答案

您做错了方法.
从不将您的数据处理代码与演示代码混合使用.

You are doing it wrong way.
Never mix your data manipulation code with presentation code.

首先创建一个函数以获取帖子:

First make a function to get posts:

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();

然后使用此数组显示

这篇关于Mysqli查询无法正常工作两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆