我想将php预处理语句的结果保存到数组中,然后将其写出来(echo)
<?php
require_once "DBinit.php";
$vpisna=$_POST['vpisna'];
$dbh = DBInit::getInstance();
$stmt = $dbh->prepare("SELECT * FROM studenti");
$stmt->bindValue(':id_student', $id_student);
$stmt->bindValue(':ime', $ime);
$stmt->bindValue(':priimek', $priimek);
$stmt->bindValue(':naslov', $naslov);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':vpisna', $vpisna);
$stmt->bindValue(':geslo', $geslo);
$stmt->bindValue(':status', $status);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//$result = mysqli_query($con,"SELECT * FROM studenti");
echo "<table border='1' width='100%'>
<tr>
<th> Št.</th>
<th>Ime</th>
<th>Priimek</th>
<th>Naslov</th>
<th>Email</th>
<th>Vpisna številka</th>
<th>Geslo</th>
<th>Status</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id_student'] . "</td>";
echo "<td>" . $row['ime'] . "</td>";
echo "<td>" . $row['priimek'] . "</td>";
echo "<td>" . $row['naslov'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['vpisna'] . "</td>";
echo "<td>" . $row['geslo'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
感谢您的帮助。
这应该写出(到浏览器中)学生列表及其信息。
我无法弄清楚如何将stmt结果保存到数组中以使用$ row读取它。
答案 0 :(得分:0)
首先,您必须使用INSERT语句插入新行...
$row = [
'ime' => 'Bob',
'email' => 'bob@example.com'
];
$sql = "INSERT INTO studenti SET ime=:ime, email=:email";
$dbh->prepare($sql)->execute($row);
...然后您可以选择并输出所有行:
$stmt = $dbh->query('SELECT * FROM studenti');
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td>" . htmlspecialchars($row['email'], ENT_QUOTES, 'UTF-8') . "</td>";
// ...
echo "</tr>";
}