如何在 HTML 块内回显转义 PHP 代码中的 MySQLi 行
How to echo a MySQLi row in escaped PHP code, inside an HTML block
我想知道在 HTML 文本块中从 MySQLi 代码回显一行的适当语法是什么。我正在处理一个页面,该页面在开始时使用 PHP 代码来确定 session 是否已启动,并且 post 评论用户已经 posted,在拉说之后来自 MySQLi 数据库的评论。有趣且令人困惑的部分是,我已经完成了我在一个 HTML div 中尝试做的事情,但我似乎无法让它在下一个中工作。
<?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 div 下方的 HTML div 中显示 $row['posts'] =] 代码,标题为 left_col。我正在尝试在下一个 div 中做完全相同的事情,但不是回显 $row['posts'],我想回显诸如 $row['store_city'] 的行以获得页面在将其从先前选择的数组中拉出后显示商店的位置。这是我针对该部分的 non-functioning 代码:
<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 中只有空的 space。我没有收到任何错误。我只是没有得到...任何东西。因此,我认为这是一个语法问题。我已经尝试了与我在 echo $row['post'] 部分所做的完全相同的事情,但它不起作用。
您面临的主要问题是,您在已经获取一次结果资源对象后再次尝试从 $data
MySQLi 结果资源对象中获取行。这不会按预期工作,因为 MySQL 保留一个内部记录集指针,每次调用 mysqli_fetch_array()
时它都会前进。因此,当到达第一个循环的末尾时,指针位于记录集的末尾,随后的调用将 return FALSE
.
因此,您的第二个循环无处可去,因为它第一次调用 mysqli_fetch_array()
returns FALSE
,退出循环。你有两个选择。
最快的解决方法是倒回记录指针 using mysqli_data_seek()
。在 second 循环之前调用,它将指针设置回第一条记录,允许您再次获取它们。
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...
}
稍后,您将使用 foreach
循环遍历 $rows
,而不是再次使用 $data
。
// 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()
。
我想知道在 HTML 文本块中从 MySQLi 代码回显一行的适当语法是什么。我正在处理一个页面,该页面在开始时使用 PHP 代码来确定 session 是否已启动,并且 post 评论用户已经 posted,在拉说之后来自 MySQLi 数据库的评论。有趣且令人困惑的部分是,我已经完成了我在一个 HTML div 中尝试做的事情,但我似乎无法让它在下一个中工作。
<?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 div 下方的 HTML div 中显示 $row['posts'] =] 代码,标题为 left_col。我正在尝试在下一个 div 中做完全相同的事情,但不是回显 $row['posts'],我想回显诸如 $row['store_city'] 的行以获得页面在将其从先前选择的数组中拉出后显示商店的位置。这是我针对该部分的 non-functioning 代码:
<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 中只有空的 space。我没有收到任何错误。我只是没有得到...任何东西。因此,我认为这是一个语法问题。我已经尝试了与我在 echo $row['post'] 部分所做的完全相同的事情,但它不起作用。
您面临的主要问题是,您在已经获取一次结果资源对象后再次尝试从 $data
MySQLi 结果资源对象中获取行。这不会按预期工作,因为 MySQL 保留一个内部记录集指针,每次调用 mysqli_fetch_array()
时它都会前进。因此,当到达第一个循环的末尾时,指针位于记录集的末尾,随后的调用将 return FALSE
.
因此,您的第二个循环无处可去,因为它第一次调用 mysqli_fetch_array()
returns FALSE
,退出循环。你有两个选择。
最快的解决方法是倒回记录指针 using mysqli_data_seek()
。在 second 循环之前调用,它将指针设置回第一条记录,允许您再次获取它们。
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...
}
稍后,您将使用 foreach
循环遍历 $rows
,而不是再次使用 $data
。
// 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()
。