简体   繁体   中英

how to echo results from a prepared sql statement in php

I am trying to echo out data from my database using a prepared statement. I am stuck on actually looping over the results to display it

Here is the code:

$sql = "select fname,lname,email,address,suburb,state,postcode,phone from customer where id = ? ";
  $stmt = $dbConn->prepare($sql);
  $stmt->bind_param("s", $user);
  $Stmt->execute();
  $rs = $stmt->get_result();

<?php while($row = $stmt->fetch_assoc()){  ?>

    <h3> Your Current Details: </h3>
    <p><strong>first name:</strong> <?php  echo $row['fname'] ; ?></p>
    <p><strong>last name:</strong> <?php echo $row['lname'] ; ?></p>
    <p><strong>email:</strong> <?php echo $row['email'] ; ?></p>
    <p><strong>post code:</strong> <?php echo $row['postcode'] ;?></p>
    <p><strong>state:</strong> <?php echo $row ['state'] ; ?></p>
    <p><strong>address:</strong> <?php echo $row ['address'] ; ?></p>
    <p><strong>suburb:</strong> <?php echo $row ['suburb'] ;?></p>
    <p><strong>phone:</strong> <?php echo $row ['phone'] ; }?></p>
    </div> 

I am new to using prepared statements and all similar posts and online post show a different method when using a prepared statement. (they use PDO)

Maybe try something like this:

$sql = "select fname,lname,email,address,suburb,state,postcode,phone from customer where id = ? ";
$stmt = $dbConn->prepare($sql);
$stmt->bind_param("s", $user);
$stmt->execute();
$rs = $stmt->get_result();

echo "<h3> Your Current Details: </h3>";
while ($row = $stmt->fetch_assoc()) {
    echo "<p><strong>first name:</strong>" . $row['fname'] . "</p>";
    echo "<p><strong>last name:</strong>" . $row['lname'] . "</p>";
    echo "<p><strong>email:</strong>" . $row['email'] . "</p>";
    echo "<p><strong>post code:</strong>" . $row['postcode'] . "</p>";
    echo "<p><strong>state:</strong>" . $row ['state'] . "</p>";
    echo "<p><strong>address:</strong>" . $row ['address'] . "</p>";
    echo "<p><strong>suburb:</strong>" . $row ['suburb'] . "</p>";
    echo "<p><strong>phone:</strong>" . $row ['phone'] . "</p>";
}
echo "</div>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM