[英]How to echo a MySQLi row in escaped PHP code, inside an HTML block
我想知道什么是合適的語法,以便在HTML文本塊中從MySQLi代碼回顯一行。 我正在研究一個頁面,該頁面從一開始就使用PHP代碼來確定會話是否已啟動,並在從MySQLi數據庫中提取用戶評論后發布用戶已發布的評論。 有趣且令人困惑的部分是,我已經完成了我試圖在一個HTML div中進行的操作,但是我似乎無法使其在下一個HTML中工作。
<?php
$page_title = "store";
require_once('connectvars.php');
require_once('startsession.php');
require_once('header.php');
// Connect to the DB
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Grab location data from the DB
// Grab post data
$query2 = "SELECT sp.post_id, sp.admin_id, sp.post, sp.date, sa.admin_id, s.store_name, s.store_city, s.store_state, s.store_zip,s.store_phone, s.store_email, s.store_address FROM store_posts sp, store_admin sa, stores s WHERE sp.admin_id='$admin_id' and sa.admin_id='$admin_id' and s.admin_id='$admin_id'ORDER BY date DESC";
$data = mysqli_query($dbc, $query2);
if (mysqli_num_rows($data) == 0) {
$blankwall = "Empty feed? '<a href=manage.php><u>Click here</u></a> to manage posts and communicate with your customers!";
}
?>
<body>
<div id="content">
<!-- BANNER & CONTENT-->
<h2>Recent Posts</h2>
<div id="store_wrap">
<div id="left_col">
<br />
<?php
**// Loop through posts and show them
if (mysqli_num_rows($data) == 0) {
echo $blankwall;
}
else {
while ($row = mysqli_fetch_array($data)) {
// Show the posts
echo '' . $row['post'] . ' | ';
echo date('M j, Y g:i A', strtotime($row['date']));
echo '<br /><hr />';
}**
}
?>
</div><!-- closes left_col -->
因此,以上所有代碼都可以查詢數據庫以獲取正確的數組,然后在PHP代碼下方名為left_col的HTML div中顯示$ row ['posts']。 我正在嘗試在下一個div中執行完全相同的操作,但我不想回顯$ row ['posts'],而是回顯了諸如$ row ['store_city']之類的行,以使頁面在拉出后顯示商店的位置從先前選擇的數組中刪除。 這是我那部分無效的代碼:
<div id="right_col">
<div id="store_picture">
<img src="images/store.jpg" style="width:325px;"/>
</div><!-- closes picture --><br />
<div id="store_address">
<br /><br /><h2>Location Info</h2>
<?php
if (mysqli_num_rows($data) == 1) {
while ($row = mysqli_fetch_array($data)) {
echo '<p>' . $row['store_city']. '</p>';
}
}
mysqli_close($dbc);
?>
</div><!-- closes store_address -->
<div id="store_info">
<p></p>
</div><!-- closes store_info -->
</div><!-- closes right_col -->
<div class="clear"></div>
</div><!-- closes store_wrap -->
</div><!-- closes content -->
無論出於何種原因,當我第二次嘗試從該數組中回顯數據時,該div中都只有空白。 我沒有任何錯誤。 我什么都沒得到。 因此,我認為這是一個語法問題。 我已經嘗試過在與我回顯$ row ['post']的部分進行相同的操作,但是它不起作用。
您面臨的主要問題是您已經嘗試了一次從$data
MySQLi結果資源對象中獲取行,然后再進行嘗試。 這將無法按預期工作,因為MySQL會保留一個內部記錄集指針,該指針在每次調用mysqli_fetch_array()
時mysqli_fetch_array()
前進。 因此,當到達第一個循環的末尾時,指針位於記錄集的末尾,隨后的調用將返回FALSE
。
因此,您的第二個循環mysqli_fetch_array()
因為它對mysqli_fetch_array()
第一次調用返回FALSE
,退出循環。 您有兩個選擇。
最快的解決方法是使用mysqli_data_seek()
倒回記錄指針。 在第二個循環之前調用,它將把指針設置回第一條記錄,使您可以再次獲取它們。
if (mysqli_num_rows($data) == 1) {
// Rewind the record pointer back to the first record
mysqli_data_seek($data, 0);
while ($row = mysqli_fetch_array($data)) {
// note addition of htmlspecialchars() to escape the string for HTML!
echo '<p>' .htmlspecialchars($row['store_city']). '</p>';
}
}
如果您的記錄集很小,那么更好的選擇是將所有行一次提取到一個數組中,然后將該數組與foreach
循環一起用於隨后的兩個輸出循環:
// To hold all rows
$rows = array();
// Do the query
$data = mysqli_query($dbc, $query2);
// (don't forget to check for errors)
if ($data) {
while ($row = mysqli_fetch_array($data)) {
// Append the row onto the $rows array
$rows[] = $row;
}
}
else{
// handle the error somehow...
}
稍后,您將不再使用$data
,而是通過foreach
循環foreach
$rows
。
// use count($rows)
if (count($rows) == 0) {
echo $blankwall;
}
else {
foreach ($rows as $row) {
// Show the posts
echo '' . $row['post'] . ' | ';
echo date('M j, Y g:i A', strtotime($row['date']));
echo '<br /><hr />';
}
}
...然后在代碼的第二個循環中再次執行相同的操作。 建議在適當的地方對查詢輸出的字符串字段使用htmlspecialchars()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.