Mysqli 查询不能工作两次

2023-03-06php开发问题
0

本文介绍了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");如果(!$查询)回声 mysqli_error();而 ($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 "";echo "

";echo "

";echo "<h2>{$post_name}</h2>";echo "<h3>{$post_date}</h3>";回声</div>";echo "<p>{$post_display}</p>";回声</div>";回声</a>";}mysqli_free_result();}函数 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");而 ($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.

    <?php all_sidebar_posts();?>

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

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

解决方案

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

首先,将帖子放入数组:

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_displayFROM blog_posts ORDER BY id DESC LIMIT 5"$result = mysqli_query($mysqli, $sql);$data = 数组();而 ($row = mysqli_fetch_assoc($result)) {$data[] = $row;}

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

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

Here is the html that I am outputting to.

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

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, get the posts into array:

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;
}

and then use this $data array to display posts any times you need, simply using foreach()

这篇关于Mysqli 查询不能工作两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用

PHP实现DeepL翻译API调用

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误

Laravel Gmail 配置错误

Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题

将 PHPMailer 用于 SMTP 的问题

Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17

热门文章

1nohup:忽略输入并将输出附加到“nohup.out" 2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET 3如何将 LDAP 时间戳转换为 Unix 时间戳 4不推荐使用常量 FILTER_SANITIZE_STRING 5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动 6PHP通过phpspreadsheet导入Excel日期数据处理方法 7Analytics API 返回:错误请求 - invalid_grant 8“tlsv1 警报内部错误"握手时

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序