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(); ?>
DB connecting:
<?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);
?>
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.
UPDATED CODE
<?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(); ?>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…