如何在 HTML 块内的转义 PHP 代码中回显 MySQLi 行


How to echo a MySQLi row in escaped PHP code, inside an HTML block

我想知道在HTML文本块中回显MySQLi代码中的一行的适当语法是什么。我正在处理一个页面,该页面在开始时使用 PHP 代码来确定会话是否已启动,并在从 MySQLi 数据库中提取该评论后发布用户已发布的评论。有趣和令人困惑的部分是,我已经在一个 HTMLdiv 中完成了我想要做的事情,但我似乎无法让它在下一个中工作。

    <?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 代码下方的 HTMLdiv 中显示$row['posts'],标题为 left_col。我正在尝试在下一个div 中做完全相同的事情,但我想回显 $row['store_city'] 等行,而不是回显 $row['posts'],以使页面在将其从先前选择的数组中拉出后显示商店的位置。这是我对该部分的不起作用的代码:

<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()时都会前进。因此,当到达第一个循环的末尾时,指针位于记录集的末尾,后续调用将返回 FALSE

因此,你的第二个循环无处可去,因为它对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...
}

稍后,您将使用 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()