問題描述
我無法同時使用 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 查詢不能工作兩次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!