Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

php - How to show all DB result with PDO

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer is as follows:

<?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 } ?>

This code will do it's work. Its completely perfect PDO and it works 100%. A friend helped my out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...