如何使用php像asp样式转义html一样循环遍历数据库

如何使用php像asp样式转义html一样循环遍历数据库,php,html,Php,Html,在asp中,我可以非常轻松地在数据库中循环,并直观地转义html(我不知道我在这里使用的术语是否正确),我的意思是,在循环数据库时,我不希望html嵌入到我的代码中,因为我喜欢使它干净,并且我认为它在某些编辑器(颜色代码)上看起来更好。然而,我不知道如何在php中做到这一点,因为我是新手。另外,使用下面的技术,我可以执行多个while循环,但我仍然坚持使用php,因为我必须在while循环中回显html,并且在每一步中都会让人困惑。这是我的资料,你可以更好地理解 我首先在asp中建立数据库连接

在asp中,我可以非常轻松地在数据库中循环,并直观地转义html(我不知道我在这里使用的术语是否正确),我的意思是,在循环数据库时,我不希望html嵌入到我的代码中,因为我喜欢使它干净,并且我认为它在某些编辑器(颜色代码)上看起来更好。然而,我不知道如何在php中做到这一点,因为我是新手。另外,使用下面的技术,我可以执行多个while循环,但我仍然坚持使用php,因为我必须在while循环中回显html,并且在每一步中都会让人困惑。这是我的资料,你可以更好地理解

我首先在asp中建立数据库连接

    <%
Set bag=Server.CreateObject("Adodb.Connection")
yol=server.mappath("database.mdb")
bag.Open "DBQ="& yol & ";Driver={Microsoft Access Driver (*.mdb)}"
%>
<table>
    <tr>
      <th>data header</th>
    </tr>
    <% do while not rs.eof %>
    <tr>
      <td><%=rs("datarowhere")%></td>
    </tr>
     <%rs.movenext
      loop %>
</table>

然后我创建记录集

 <%
Set rs = Server.CreateObject("ADODB.Recordset")
sql = "SELECT * from data"
rs.Open sql, bag, 1, 3
%>

然后假设我正在使用表格进行while循环

    <%
Set bag=Server.CreateObject("Adodb.Connection")
yol=server.mappath("database.mdb")
bag.Open "DBQ="& yol & ";Driver={Microsoft Access Driver (*.mdb)}"
%>
<table>
    <tr>
      <th>data header</th>
    </tr>
    <% do while not rs.eof %>
    <tr>
      <td><%=rs("datarowhere")%></td>
    </tr>
     <%rs.movenext
      loop %>
</table>

数据头
我可以看到我的html,我可以很容易地看到我的数据

现在的问题是,在php中,html是完全独立的,而不是嵌入在echo中(假设表名为data,行为datarowhere),我如何在php中做同样的事情?可能吗?如果不是,最好的方法是什么?

您可以这样做

<table>
<tr>
  <th>data header</th>
</tr>
<?php while (some condition) { ?>
<tr>
  <td><?php echo 'something'; ?></td>
</tr>
 <?php
    }
 ?>
</table>

数据头

它将php和html代码分开

以下是如何使用php中的类连接到mysql数据库:

/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
  $rs = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}
建立连接后,您可以发表声明、声明、声明和结果:

$sql = "SELECT * from data";
$sth = $rs->prepare($sql);

if ($sth->execute()) {
  $allRows = $sth->fetchAll();
} else {
  die('There was a problem with your database query.');
}
然后,在HTML中,您可以使用一个循环遍历要将数据库中的信息添加到的部分,并在其中回显信息:

<table>
  <tr>
    <th>data header</th>
  </tr>
  <?php foreach ($allRows as $row) { ?>
    <tr>
      <td><?php echo $row["datarowhere"]; ?></td>
    </tr>
  ?>
</table>

数据头
?>
您必须查看数据是如何从fetchAll()语句返回的,这样才能正确地访问它并将其返回

以下是完整的示例:

<?php

/* Connect to a MySQL database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
  $rs = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
  echo 'Connection failed: ' . $e->getMessage();
}

$sql = "SELECT * from data";
$sth = $rs->prepare($sql);

if ($sth->execute()) {
  $allRows = $sth->fetchAll();
} else {
  die('There was a problem with your database query.');
}

?>
<table>
    <tr>
      <th>data header</th>
    </tr>
    <?php foreach ($allRows as $row) { ?>
      <tr>
        <td><?php echo $row["datarowhere"]; ?></td>
      </tr>
    ?>
</table>

数据头
?>

我想这正是我想要的。非常感谢,没想到一旦建立了连接就会这么容易。我也会尝试一下while loop,谢谢你的友好回复。@muratkh不用担心,巴德:)