如何使用PDO显示所有数据库结果 [英] How to show all DB result with PDO
本文介绍了如何使用PDO显示所有数据库结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以, 我刚开始使用PDO时,连接和东西工作得很好,但是现在我有一个小问题.我被困在一个部分,希望在每个表中显示6个结果.我的代码如下:
So, I just started with PDO and the connection and stuff work great, but now I have a little problem. I'm stuck on a part where I want to show 6 results per table. My code is as following:
<?php
$sql = "SELECT * FROM db WHERE id BETWEEN 1 AND 6";
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':hours', $hours);
try {
$stmt = $conn->prepare($sql);
$result = $stmt->execute($parameters);
} while($row = $result->fetch_assoc()){ ?>
<tr>
<td><b><?php echo $row['hours'] ?></b></td>
<td><a href="#"></a></td>
<td id="dayhour-1">
<input placeholder="Name" type="text" class="form-control" id="1" value="<?php echo $row['userName'] ?>">
</td>
</tr>
<?php } $stmt->close(); ?>
数据库连接:
<?php
$db_host = "localhost";
$db_name = "xxx";
$db_user = "xxx";
$db_pass = "xxx";
$db_opts = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
);
$conn = new PDO("mysql:host=$db_host;dbname=$db_name;charset=utf8mb4", $db_user, $db_pass, $db_opts);
?>
当我转到网页时,它显示了众所周知的500错误.我不知道我在做什么错,因为我是一个初学者.
请让我知道我做错了什么以及如何解决此问题.
When I go to the webpage, it is showing the well-known 500 error. I have no idea what I am doing wrong because I am a starter.
Please let me know what I am doing wrong and how I can solve this problem.
更新代码
<?php
$sql = "SELECT * FROM db WHERE id BETWEEN 1 AND 6";
$stmt->bindParam(':userName', $userName);
$stmt->bindParam(':hours', $hours);
try {
$stmt = $conn->prepare($sql);
$result = $stmt->execute($stmt);
} catch ($row = $result->fetch() {
?>
<tr>
<td><b><?php echo $row['hours'] ?></b></td>
<td><a href="#"></a></td>
<td id="dayhour-1">
<input type="text" value="<?php echo $row['userName'] ?>">
</td>
</tr>
<?php } $stmt->close(); ?>
推荐答案
答案如下:
<?php
$sql = "SELECT * FROM database WHERE id BETWEEN 1 AND 5";
$stmt = $conn->query($sql);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJ)){
?>
<tr>
<td><b><?php echo $row->hours ?></b></td>
<td><a href="#"></a></td>
<td id="dayhour-1">
<input type="text" class="form-control" id="1" value="<?php echo $row->username ?>">
</td>
</tr>
<?php } ?>
此代码将起作用.其完全完美的PDO,并且可以100%工作.一个朋友帮助了我.
This code will do it's work. Its completely perfect PDO and it works 100%. A friend helped my out.
这篇关于如何使用PDO显示所有数据库结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文